PHP 输出控制

巴扎黑
发布: 2016-11-11 10:04:10
原创
1266人浏览过

最近提交完代码后,发现firephp在其他人的环境下又出问题了,提示:’headers already sent in …’,与上一次nginx 缓冲区超出不太一样。查看nginx错误日志,并没有发现错误,并且有同学发现apache下面也会,怀疑是php的问题。但是我用的也是apache,并不会出现问题!偶然发现有一个页面不会出现错误提示,发现该页面输出内容大小在1kb左右,怀疑是php的输出缓冲区超出时,自动发送缓冲区数据,导致后续firephp通过http header发送调试信息失败了并结束php脚本执行。

 

立即学习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
登录后复制

 

立即学习PHP免费学习笔记(深入)”;

根据上面的解释,当output_buffering为On或者4096时,在每次请求里面,当我们使用echo或print的时候,php实际上并不立即输出而是先保存到缓冲区里面去,当达到一定大小(如4KB)或脚本执行结束的时候,再向浏览器输出缓冲区内容并清空。

 

立即学习PHP免费学习笔记(深入)”;

Http协议传输内容时,先传输响应头,一旦内容开始输出后,响应头不再可以改变。当output_buffering为On时,PHP会将所有的输出缓存起来,等待请求结束时在向浏览器输出内容,故Firephp在最后时刻更改Http响应头仍然不会存在问题,因为此时仍输出任何内容;当output_buffering为4096(或其他固定值)时,每次php缓冲区一满便会向客户端输出,此时已输出内容,响应头不再可以改变,若尝试设置header便会提示:’Headers already sent…’。

 

立即学习PHP免费学习笔记(深入)”;

做好Webserver的缓冲输出控制,能够带来更好的用户体验,如Facebook、新浪的Bigpipe。

 

立即学习PHP免费学习笔记(深入)”;

本次的问题排查发现,由于大部分页面的调试信息较多,导致Http响应头较大(远大于4KB,有的达到36KB),所以触发了自动输出机制。解决办法:更改output_buffering为On;在代码里面手动ob_start();更改output_buffering为更大值;减少日志调试信息。

PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号