PHP Output Control

Source: Internet
Author: User
After recently submitted the code, found that firephp in other people's environment again, prompted: ' Headers already sent in ... ', with the last Nginx buffer exceeded the same. View Nginx error log, and did not find errors, and some students found that Apache will also be, suspected of PHP problems. But I also use Apache, and there will be no problem! Accidentally found that there is a page does not appear error prompt, found that the page output content size around 1KB, suspect that PHP output buffer exceeded, automatically send buffer data, resulting in subsequent firephp send debug information through the HTTP header failed and end the PHP script execution.

Comment out the output statement after the template is rendered, and no longer prompt for the error, to determine if there is a problem with the buffered output of PHP. Compare the php.ini in different environments and find that the value of output_buffering is not the same, my value is on, while others are the default value of 4096.

PHP code

 Output buffering is a mechanism for controlling what much output data (excluding headers and cookies) PHP should keep in Ternally before pushing that data to the client.   If your application ' s output exceeds this setting, PHP would send that data in chunks of roughly the size of your specify. Turning on this setting and managing it maximum buffer size can yield some interesting side-effects depending on your   Application and Web server. You are able to send headers and cookies after you've already sent output through print or echo. You also could see performance benefits if your server was emitting less packets due to buffered output versus PHP streamin G The output as it gets it.   On production servers, 4096 bytes are 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 Val ue:4096 http://php.net/output-buffering output_buffering = On

According to the above explanation, when output_buffering is on or 4096, in each request, when we use echo or print, PHP does not actually output immediately but is saved in the buffer. When a certain size (such as 4KB) or the end of script execution is reached, output the buffer contents to the browser and empty it.

When the HTTP protocol transmits content, the response header is transmitted first, and once the content begins to output, the response header can no longer be changed. When Output_buffering is on, PHP caches all the output and waits for the request to be output to the browser at the end, so firephp changes the HTTP response header at the last minute is still not problematic, since nothing is output at this time; when Output_ Buffering is 4096 (or other fixed value), each time the PHP buffer full will be output to the client, when the output, the response header can no longer change, if you try to set the header will prompt: ' Headers already sent ... '.

A good webserver buffer output control can bring a better user experience, such as Facebook, Sina's bigpipe.

This troubleshooting found that most of the page debug information, resulting in a large HTTP response header (much larger than 4KB, some 36KB), so triggered the automatic output mechanism. Workaround: Change output_buffering to ON, manual Ob_start () in code, change output_buffering to greater value, reduce log debug information.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.