Output_buffering in php. ini

Source: Internet
Author: User
Tags ack flush sleep time interval blank page drupal

Php output_buffering

By default, php buffer is enabled, and the default value of this buffer is 4096, that is, 4 KB. You can use. in the ini configuration file, find output_buffering configuration. when echo, print, and other user data are output, the output data will be written to php output_buffering until output_buffering is fully written. The data will be transmitted to the browser over tcp for display. You can also manually activate php output_buffering through ob_start (), so that even if the output exceeds 4 kB, the data is not really sent to the browser over tcp, because ob_start () set the php buffer space to large enough. Data is sent to the client browser only after the script is completed or the ob_end_flush function is called.

1. When output_buffering = 4096, and a small amount of data is output (less than one buffer)

The code is as follows: Copy code

<? Php
For ($ I = 0; $ I <10; $ I ++ ){
Echo $ I. '<br/> ';
Sleep ($ I + 1 );//
}
?>

Symptom: no intermittent output occurs every few seconds, but the output can be viewed at a time until the response ends. The browser interface remains blank until the server script processing ends. This is because the data volume is too small, and php output_buffering is not full. Data writing order, which is echo-> php buffer-> tcp buffer-> browser

2. When output_buffering = 0, and a small amount of data is output (less than one buffer)

The code is as follows: Copy code
<? Php
// Ini_set ('output _ buffering ', 0) does not take effect
// Edit/etc/php. ini and set output_buffering = 0 to disable the output buffering mechanism.
// Ini_set ('output _ buffering ', 0); // completely disable the output buffering function
For ($ I = 0; $ I <10; $ I ++ ){
Echo $ I. '<br/> ';
Flush (); // notifies the underlying operating system to send data to the client browser as soon as possible
Sleep ($ I + 1 );//
}
?>

Symptom: it is inconsistent with the display just now. After the php buffering mechanism is disabled, intermittent output can be seen intermittently in the browser, and the output does not have to be seen until the script is executed. This is because the data is not stuck in php output_buffering. The data write order is echo-> tcp buffer-> browser.

3. When output_buffering = 4096. The output data is greater than a buffer and ob_start () is not called ()

# // Create a 4 kB file
$ Dd if =/dev/zero of = f4096 bs = 4096 count = 1

The code is as follows: Copy code
<? Php
For ($ I = 0; $ I <10; $ I ++ ){
Echo file_get_contents ('./f4096'). $ I. '<br/> ';
Sleep ($ I + 1 );
}
?>

Symptom: the response is not over yet (the http connection is not closed). Intermittent output is displayed intermittently, and the browser interface is not left blank. Although the php output_buffering mechanism is enabled, the output will still be intermittent, rather than one-time, because the output_buffering space is insufficient. Every time a php buffering is filled, the data is sent to the client browser.

4. When output_buffering = 4096, the output data is greater than a tcp buffer and ob_start () is called ()

The code is as follows: Copy code
<? Php
Ob_start (); // enable php buffer
For ($ I = 0; $ I <10; $ I ++ ){
Echo file_get_contents ('./f4096'). $ I. '<br/> ';
Sleep ($ I + 1 );
}
Ob_end_flush ();
?>

Symptom: The complete output is not displayed until the script processing on the server end and the response is complete. The output interval is very short, so that you cannot feel the pause. Before the output, the browser maintains a blank page, waiting for the server data. This is because, once php calls the ob_start () function, it will extend the php buffer to a large enough value, the data in the php buffer is sent to the client browser only after the ob_end_flush function call or the completion speed of script running.

Tcpdump observation
Here, we use tcpdump to Monitor tcp packets and observe the difference between using ob_start () and not using it.
1. ob_start () is not used ()

12:30:21. 499528 IP 192.168.0.8.webcache> 192.168.0.28.cymtec-port:. ack 485 win 6432
12:30:21. 500127 IP 192.168.0.8.webcache> 192.168.0.28.cymtec-port:. 1: 2921 (2920) ack 485 win 6432
12:30:21. 501000 IP 192.168.0.8.webcache> 192.168.0.28.cymtec-port:. 2921: 7301 (4380) ack 485 win 6432
12:30:21. 501868 IP 192.168.0.8.webcache> 192.168.0.28.cymtec-port: P 7301: 8412 (1111) ack 485 win 643
12:30:24. 502340 IP 192.168.0.8.webcache> 192.168.0.28.cymtec-port:. 8412: 14252 (5840) ack 485 win 6432
12:30:24. 503214 IP 192.168.0.8.webcache> 192.168.0.28.cymtec-port:. 14252: 15712 (1460) ack 485 win 6432
12:30:24. 503217 IP 192.168.0.8.webcache> 192.168.0.28.cymtec-port: P 15712: 16624 (912) ack 485 win 6432

12:30:31. 505934 IP 192.168.0.8.webcache> 192.168.0.28.cymtec-port:. 16624: 23924 (7300) ack 485 win 6432
12:30:31. 506839 IP 192.168.0.8.webcache> 192.168.0.28.cymtec-port: P 23924: 24836 (912) ack 485 win 6432
12:30:42. 508871 IP 192.168.0.8.webcache> 192.168.0.28.cymtec-port:. 24836: 32136 (7300) ack 485 win 6432
12:30:42. 509744 IP 192.168.0.8.webcache> 192.168.0.28.cymtec-port: P 32136: 33048 (912) ack 485 win 6432
12:30:57. 512137 IP 192.168.0.8.webcache> 192.168.0.28.cymtec-port:. 33048: 40348 (7300) ack 485 win 6432
12:30:57. 513016 IP 192.168.0.8.webcache> 192.168.0.28.cymtec-port: P 40348: 41260 (912) ack 485 win 6432
12:31:06. 513912 IP 192.168.0.8.webcache> 192.168.0.28.cymtec-port: P 41260: 41265 (5) ack 485 win 6432
12:31:06. 514012 IP 192.168.0.8.webcache> 192.168.0.28.cymtec-port: F 41265: 41265 (0) ack 485 win 6432
12:31:06. 514361 IP 192.168.0.8.webcache> 192.168.0.28.cymtec-port:. ack 486 win 6432
2. ob_start () is used ()

12:36:06. 542244 IP 192.168.0.8.webcache> 192.168.0.28.noagent:. ack 485 win 6432
12:36:51. 559128 IP 192.168.0.8.webcache> 192.168.0.28.noagent:. 1: 2921 (2920) ack 485 win 6432
12:36:51. 559996 IP 192.168.0.8.webcache> 192.168.0.28.noagent:. 2921: 7301 (4380) ack 485 win 6432
12:36:51. 560866 IP 192.168.0.8.webcache> 192.168.0.28.noagent:. 7301: 11681 (4380) ack 485 win 6432
12:36:51. 561612 IP 192.168.0.8.webcache> 192.168.0.28.noagent:. 11681: 16061 (4380) ack 485 win 6432
12:36:51. 561852 IP 192.168.0.8.webcache> 192.168.0.28.noagent:. 16061: 20441 (4380) ack 485 win 6432
12:36:51. 562479 IP 192.168.0.8.webcache> 192.168.0.28.noagent:. 20441: 24821 (4380) ack 485 win 6432
12:36:51. 562743 IP 192.168.0.8.webcache> 192.168.0.28.noagent:. 24821: 29201 (4380) ack 485 win 6432
12:36:51. 562996 IP 192.168.0.8.webcache> 192.168.0.28.noagent:. 29201: 33581 (4380) ack 485 win 6432
12:36:51. 563344 IP 192.168.0.8.webcache> 192.168.0.28.noagent: P 33581: 35041 (1460) ack 485 win 6432
12:36:51. 563514 IP 192.168.0.8.webcache> 192.168.0.28.noagent:. 35041: 36501 (1460) ack 485 win 6432
12:36:51. 563518 IP 192.168.0.8.webcache> 192.168.0.28.noagent:. 36501: 37961 (1460) ack 485 win 6432
12:36:51. 563523 IP 192.168.0.8.webcache> 192.168.0.28.noagent:. 37961: 39421 (1460) ack 485 win 6432
12:36:51. 563526 IP 192.168.0.8.webcache> 192.168.0.28.noagent:. 39421: 40881 (1460) ack 485 win 6432
12:36:51. 563529 IP 192.168.0.8.webcache> 192.168.0.28.noagent: FP 40881: 41233 (352) ack 485 win 6432
12:36:51. 570364 IP 192.168.0.8.webcache> 192.168.0.28.noagent:. ack 486 win 6432
Through the comparison above, we can see that the time interval of data packets is significantly different. If ob_start () is not used, the time interval is large. Wait about 4 seconds to send the data in tcp buffer. If the data remains in the php buffer for a long time, the output data is sent to the client browser. This is because the php buffer will soon be fully written and the data has to be sent out. When ob_start () is enabled, data packets are sent to the client at almost the same time. This can be inferred that the data stays in the php buffer until ob_end_flush () is called to send the data in the php buffer to the client browser.

Output buffering function
1. ob_start
Activate the output_buffering mechanism. Once activated, the script output is not directly sent to the browser, but is temporarily written to the php buffer memory area.

By default, php enables the output_buffering mechanism. However, by calling the ob_start () function, the value of output_buffering is extended to a large enough value. You can also specify $ chunk_size to specify the value of output_buffering. $ Chunk_size: The default value is 0, indicating that data in php buffer will not be sent to the browser until the script stops running. If you set the size of $ chunk_size, it indicates that as long as the data length in the buffer reaches this value, the data in the buffer will be sent to the browser.

Of course, you can specify $ ouput_callback to process data in the buffer. For example, the ob_gzhandler function compresses the data in the buffer and then transmits it to the browser.

2. ob_get_contents
Obtain a copy of data in the php buffer. It is worth noting that you should call this function in the ob_end_clean () function, otherwise ob_get_contents () returns an empty character.

3. ob_end_flush and ob_end_clean
These two functions are similar, and the ouptu_buffering mechanism is disabled. But the difference is that ob_end_flush only flush the data in the php buffer to the client browser, while ob_clean_clean clears the data in the php bufeer (erase), but does not send it to the client browser. After ob_end_flush is called, data in php buffer still exists, and ob_get_contents () can still obtain data copies in php buffer. After ob_end_clean () is called, ob_get_contents () gets an empty string, and the browser cannot receive the output, that is, no output.

Typical cases
Ob_start () is often used in some template engines and page file caches. They can be found in well-known open-source projects such as wordpress, drupal, and smarty. Here, the drupal application is extracted.

# Template File

The code is as follows: Copy code

// @ File: user-profile.tpl.php
<Div>
<Ul>
<Li> username: <? Php echo $ user-> name;?> </Li>
<Li> picture: <? Php echo $ user-> picture;?> </Li>
</Ul>
</Div>
 
// @ File: template-render.php
<? Php
Function theme_render_template ($ template_file, $ variables ){
If (! Is_file ($ template_file) {return "";}
Extract ($ variables, EXTR_SKIP );
Ob_start ();
$ Contents = ob_get_contents ();
Ob_end_clean ();
Return $ contents;
}
?>
 
// @ File: profile. php
<? Php
$ Variables = array ('user' => $ user );
Print theme_render_template ('User-profile. tpl. Php', $ variables );
?> Conclusion

 


Ob_flush/flush descriptions in the manual are used to refresh the output buffer and must be used together, which may lead to many confusion...

In fact, they have different operation objects. In some cases, flush does nothing at all ..

Ob _ * series functions are used to operate the output buffer of PHP itself.

Therefore, ob_flush is used to refresh the PHP buffer.

But flush, strictly speaking, this is only useful when PHP is installed as an apache Module (handler or filter. it is used to refresh the buffer zone of the WebServer (which can be considered to be specific to apache.

In the sapi of apache module, flush indirectly calls apache api: ap_rflush to refresh apache output buffer by calling the flush member function pointer of sapi_module, some other modules of apache may change the result of this action ..

1. Some Apache modules, such as mod_gzip, may cache output by themselves,
2. This will cause the results generated by the flush () function to not be immediately sent to the client browser.
3.
4. Even the browser caches the received content before it is displayed. For example, Netscape
5. The browser caches content before receiving a line break or the beginning of the html tag, and
6. The entire table is not displayed until the </table> Mark is received.
7.
8. Some versions of Microsoft Internet Explorer only receive 256
9. The page is displayed only after the byte, so some extra spaces must be sent for this
10. Some browsers display the page content.
Therefore, the correct order of the two users is: first ob_flush, then flush,

Of course, other SAPIs do not call flush, but we recommend that you use it to ensure the portability of your code.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.