PHP 输出控制
注释掉模板渲染后的输出语句,不再提示该错误,确定是php的缓冲输出有问题。比较不同环境下的php.ini,发现output_buffering的值不太一样,我的值是On,而其他人则是默认值4096。
Php代码
Output buffering is a mechanism for controlling how much output data (excluding headers and cookies) PHP should keep internally before pushing that data to the client. If your application's output exceeds this setting, PHP will send that data in chunks of roughly the size you specify. Turning on this setting and managing its maximum buffer size can yield some interesting side-effects depending on your application and web server. You may be able to send headers and cookies after you've already sent output through print or echo. You also may see performance benefits if your server is emitting less packets due to buffered output versus PHP streaming the output as it gets it. On production servers, 4096 bytes is a good setting for performance reasons. Note: Output buffering can also be controlled via Output Buffering Control functions. Possible Values: On = Enabled and buffer is unlimited. (Use with caution) Off = Disabled Integer = Enables the buffer and sets its maximum size in bytes. Note: This directive is hardcoded to Off for the CLI SAPI Default Value: Off Development Value: 4096 Production Value: 4096 http://php.net/output-buffering output_buffering = On
根据上面的解释,当output_buffering为On或者4096时,在每次请求里面,当我们使用echo或print的时候,php实际上并不立即输出而是先保存到缓冲区里面去,当达到一定大小(如4KB)或脚本执行结束的时候,再向浏览器输出缓冲区内容并清空。
Http协议传输内容时,先传输响应头,一旦内容开始输出后,响应头不再可以改变。当output_buffering为On时,PHP会将所有的输出缓存起来,等待请求结束时在向浏览器输出内容,故Firephp在最后时刻更改Http响应头仍然不会存在问题,因为此时仍输出任何内容;当output_buffering为4096(或其他固定值)时,每次php缓冲区一满便会向客户端输出,此时已输出内容,响应头不再可以改变,若尝试设置header便会提示:’Headers already sent…’。
做好Webserver的缓冲输出控制,能够带来更好的用户体验,如Facebook、新浪的Bigpipe。
本次的问题排查发现,由于大部分页面的调试信息较多,导致Http响应头较大(远大于4KB,有的达到36KB),所以触发了自动输出机制。解决办法:更改output_buffering为On;在代码里面手动ob_start();更改output_buffering为更大值;减少日志调试信息。
上一篇: PHP个人中心头像上传演示和下载
下一篇: PHP实现最简单爬虫原型