PHP output buffering

Source: Internet
Author: User

PHP output buffering

2017-03-02 22:41:04

the first thing to know is that  PHP,WebServer (Apacheserver) and the browser have their own buffers (Buffer), these three output data will first save the data to its own buffer, to its own buffer after being filled with data or storing a certain amount of data, the data will be output "Note: The buffer of each browser requires different amount of data stored,IEto be256Bytes, Chromewith theFireFoxto be1000Bytes,only the output data reaches this length or the script end browser will output the data on the page". PHP Program output content (echo,print_r ... There are a total of the following steps to the browser:

Echo , Print + php output_buffering = webServer buffer = Browser buff = Browser display

namely: Script output = = php buffer settings = buffer settings for the system (Apache,nginx) = = The browser's buffer setting = = is displayed to the user

the specific steps are as followsPHPBuffer (PHP Buffer)PHP  The result of the run is put in buffer, and the data is exportedonly if the buffer is full or if php is finished running. The buffer switch is passedphp.iniin theoutput_bufferingvariable control. In thephp.ini, we can set theoutput_bufferingis greater than0values to open theBuffer. In addition we can set the "Output_buffering=off"to closePHPbuffer, and then through theOb_start ()function to openPHPbuffer. "Note:Ini_set ()function cannot be modifiedPHP Bufferthe Settings " Everyone said:Ob_start () is to open php buffer ,ob_end_flush () is the php buffer is off. It is important to note that if php buffer is closed in php.ini , call Ob_end_flush again () will report Warning . WebServer Buffers (Webserver Buffer)It's only about  The buffer of the Apache server. when the PHP output data to the Apache Server, it will also do a layer of buffer (also put the data into its buffer, Data is only output when the buffer data is full or executed. To close the buffer, flush () can be used at the php layer to force the buffer data to be output from the Apache buffer. Flush () working principle: inApache Moduleof theSAPIunder, Flushis called by theSapi_moduleof theFlushmember function Pointers, Indirect invocationApacheof theApi:ap_rflushRefreshApachethe output buffer.   Of course the manual also says that there are other apache modules that may change the results of this action, such as Mod_gzip: It is possible to make an output buffer yourself, which will cause the flush () function to produce results that are not immediately sent to the client browser. Browser buffers (Browser Buffer)the data that the browser receives from the server is not displayed directly, but the data is saved in the browser buffer until the saved data reaches a certain length (different browser lengths,  IE is 256Bytes, Chrome and FireFox are 1000Bytes), The buffer of the browser is equivalent to filling up, only the data reaches this length or the script end browser will output the data on the page. in php , you can not control the browser buffer switch, if you want to output data is not affected by the browser buffer, you may send some space to fill the browser's buffer, When the browser's buffer is full, the other new output data is not directly output through the buffer. However, different browsers will set different buffer sizes. In order to insure the period, can send 4096 a space, because currently the popular browser's buffer has not more than 4k ( an inner page size ). In php.ini , we can disable the php buffer in the Apache The following code is executed under the server to explore the three buffers discussed above
1<?PHP2          Ob_start();#Open PHP Buffer3          Echo str_repeat(' &nbsp; ', 4096);#fills the browser's buffer so that the browser can output the content directly after it4          Echo' <br> ';5 6           for($i= 0;$i< 5;$i++) {7                    Echo $i, ' <br> ';8                    //Ob_flush (); #刷新php缓冲区, the data is not stuck in the PHP buffer, directly output9                    Flush();#refreshes (flushes) the buffer of the Web server, which can be specified in Apache's bufferTen                    Sleep(1);#pause for 1 seconds One          } A?>

Reference Source: http://blog.csdn.net/fwkjdaghappy1/article/details/25402809

PHP output buffering

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.