当不存在会话时 ob_gzhandler 返回空白页
我想压缩我的页面,所以我把它放在
ob_start('ob_gzhandler');
php 头文件的开头。然而,当我在玩的时候,我注意到如果我在没有使用 ob_gzhandler 的情况下启动会话,它会给我一个空白页! 所以我做了下面的事情并且它起作用了:
<?php
if (session_id() === null ) ob_start('ob_gzhandler');
?>
但是我不确定这是否总是使用压缩,因为我想象它们可能是一个会话,但浏览器仍然会收到未压缩的数据!我想如果这种情况发生,我将不得不重新启动会话,这实际上不应该除了开发时间之外。但我想我真正想知道的是一些关于 zlib 和这种压缩的快速提示。有什么性能技巧吗?我假设如果文件末尾没有指示,服务器会输出内部缓冲区,但我应该在那里放一个吗? ob_end_flushob_end_clean?ob_end_close?
关于使用 memcache 的任何注释?任何提示和信息将不胜感激!
I wanted to compress my pages so I put
ob_start('ob_gzhandler');
at the beginning of my php header file. however as I was playing around I noticed it will give me a blank page if I start a session while the ob_gzhandler was not being used!
so I did the below and it worked:
<?php
if (session_id() === null ) ob_start('ob_gzhandler');
?>
how ever I'm not sure if this will always use the compression as I imagine their might be a session but the browser will still receive uncompressed data! I guess I would have to restart session if the scenario happens which really shouldnt other than development time. but I guess what I really want to know is some quick tips about zlib and this compression. Is there any performance tips? I assume the server output the internal buffer if there is no indication at end of file but should I put one there? ob_end_flushob_end_clean?ob_end_close?
any notes on using memcache with this? any tips and info would be highly appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 ob_gzhandler() 压缩数据之前,它将检查浏览器将接受什么类型的编码,如果是 gzip,或者 deflate (也许还有其他东西,我不确定)它将打印压缩后的输出。支持所有流行的浏览器,即使您编写自己的代码或担心搜索引擎蜘蛛,HTTP 请求者也需要发送正确的 HTTP 标头,表明它接受压缩页面。
不用担心,如果浏览器不支持压缩页面,此函数将返回 FALSE 并且不应用压缩。
这是 php man 的其他一些内容:
ob_start
该函数将打开输出缓冲。当输出缓冲处于活动状态时,脚本不会发送任何输出(标头除外),而是将输出存储在内部缓冲区中。
可以使用 ob_get_contents() 将此内部缓冲区的内容复制到字符串变量中。要输出存储在内部缓冲区中的内容,请使用 ob_end_flush()。或者,ob_end_clean() 将默默地丢弃缓冲区内容。
警告
某些Web 服务器(例如Apache)在调用回调函数时会更改脚本的工作目录。您可以通过回调函数中的 chdir(dirname($_SERVER['SCRIPT_FILENAME'])) 将其更改回来。
输出缓冲区是可堆叠的,也就是说,您可以在另一个 ob_start() 处于活动状态时调用 ob_start()。只需确保调用 ob_end_flush() 适当的次数即可。如果多个输出回调函数处于活动状态,则输出将按嵌套顺序依次通过每个回调函数进行过滤。
同样,这部分是从 php 手册中复制的!
Before ob_gzhandler() compresses the data, it will check to see what type of encoding the browser will accept, if its gzip, or deflate (maybe other things too, im not sure) it will print the output with that compression. All popular browsers are supported and even if you write you own code or are worried about search engine spiders it is up to the HTTP requester to send the correct HTTP header saying that it accepts compressed pages.
And no worries, if a browser doesn't support compressed pages this function returns FALSE and no compression is applied.
here is some other stuff From the php man:
ob_start
This function will turn output buffering on. While output buffering is active no output is sent from the script (other than headers), instead the output is stored in an internal buffer.
The contents of this internal buffer may be copied into a string variable using ob_get_contents(). To output what is stored in the internal buffer, use ob_end_flush(). Alternatively, ob_end_clean() will silently discard the buffer contents.
Warning
Some web servers (e.g. Apache) change the working directory of a script when calling the callback function. You can change it back by e.g. chdir(dirname($_SERVER['SCRIPT_FILENAME'])) in the callback function.
Output buffers are stackable, that is, you may call ob_start() while another ob_start() is active. Just make sure that you call ob_end_flush() the appropriate number of times. If multiple output callback functions are active, output is being filtered sequentially through each of them in nesting order.
Again this part is copied from the php manual!