PHP output buffer. In PHP, when echo and print are executed, the output is not immediately transmitted to the client browser through tcp, but is written to phpbuffer. The phpoutput_buffering mechanism means that in tcp in PHP, when echo and print are executed, the output is not immediately transmitted to the client browser through tcp, but the data is written to the php buffer. The php output_buffering mechanism means that a new queue is established before the tcp buffer, and data must pass through the queue. When a php buffer is full, the script process sends the output data in the php buffer to the system kernel and sends it to the browser for display. Therefore, the data will be written to these places in sequence echo/pring-> php buffer-> tcp buffer-> browser
In PHP, there are three functions related to the refresh buffer:
1). flush
Refresh the buffer of the PHP program, regardless of the circumstances in which PHP is executed. This function sends all the output of the program so far to the user's browser. However, this function does not affect the cache mode of the server or client browser, nor does it affect the cache of PHP itself.
2). ob_flush
This function outputs the PHP Cache. The cache of PHP is determined byOutput_buffering. Ob_flush () is used to extract the content that already exists in the output cache and set it to wait for the output status, but it is not directly sent to the client. in this case, you need to use ob_flush () first () then use flush () to obtain the script output immediately.
The two PHP configurations related to the PHP output buffer are:
Parameter 1:Output_buffering : On/offOr integer. When it is set to on, the output cache is used in all scripts without limiting the cache size. When it is set to an integer, for example, output_buffering = 4096, when the cache data size reaches 4096 bytes, the cache is automatically refreshed. The difference in this parameter is the reason for the different execution results of the above code at different times. When output_buffering is disabled, all the output (echo) of the script will be sent to the client immediately. when the above code is executed, a number is output every second. After output_buffering is enabled, the output content is cached on the server and sent to the client together until the script ends.
Parameter 2:Implicit_flush: On/off. Setting ON means that when the script has output, it is automatically sent to the client immediately. It is equivalent to automatically adding flush () after echo ().
3). ob_implicit_flush
This function forces the output to be immediately sent to the browser whenever there is an output. In this way, you do not need to use flush () to send the output (echo) to the browser.
Example
- Ob_end_clean ();
- EchoStr_pad ("",256 );
- For($ I = 100;$ I> 0;$ I --){
- Echo$ I,'
';
- Flush ();
- Sleep (1 );
- }
- ?> The above code should be output every second $ I.The preceding echo str_pad ("", 256) aims to display IE only after receiving 256 bytes. The above code is written in either of the following ways.
- EchoStr_pad ("",256 );
- For($ I = 100;$ I> 0;$ I --){
- Echo$ I,'
/> ';
- Ob_flush (); // sometimes only flush does not work.
- Flush ();
- Sleep (1 );
- }
- ?>
View plain
- Ob_implicit_flush (true );
- EchoStr_pad ("",256 );
- For($ I = 100;$ I> 0;$ I --){
- Echo$ I,'
/> ';
- Ob_flush ();
- Sleep (1 );
- }
- ?>
In addition, we also need to note that the refresh buffer is not only affected by the above aspects, but also by the following:
1) some web server programs, especially the web server programs under Win32, will cache the script output until the program ends before sending the results to the browser. Some Apache modules, such as mod_gzip, May output the cache by themselves, which causesFlush ()The result of the function is not immediately sent to the client browser. Even the browser caches the received content before it is displayed. For example, the Netscape browser caches content before receiving a line break or the beginning of an html tag, and receivesThe entire table is not displayed until it is marked. In some versions of Microsoft Internet Explorer, the page is displayed only after 256 bytes are received. Therefore, some extra spaces must be sent for these browsers to display the page content.
Below is a simple piece of code
Cache_file = $ cache_file; $ this-> cache_time = $ cache_time;} // cache start function cache_start () {if ($ this-> cache_active) {include ($ this-> cache_file); exit ;}// enable cache ob_start () ;}// determine whether the cached file exists and the function cache_active () is available () {// Determine whether the file exists if (file_exists ($ this-> cache_file) {$ last_time = @ filemtime ($ this-> cache_file ); // Obtain the last modification time // Determine whether the time is available if ($ this-> cache_time <$ last_time) {// available, including incoming and direct display of return true ;} else {// delete the cache and recreate the cache unlink ($ this-> cach E_file); return false ;}}// function cache_creat () generated by the cache directory {// The cache file directory and file are directly generated without judgment, cyclically generated file $ file = explode ("/", $ this-> cache_file); $ num = count ($ file)-1; for ($ I = 0; $ I <$ num; $ I ++) {$ tm. = $ file [$ I]. "/"; if (! File_exists ($ tm) {mkdir ($ tm) ;}}// cached output function cache_end () {$ cache_content = ob_get_contents (); $ this-> cache_creat (); $ fp = @ fopen ($ this-> cache_file, "w +"); fwrite ($ fp, $ cache_content); ob_end_flush ();} // Cache clearing function cache_clean () {if (unlink ($ this-> cache_file) {return true;} else {$ this-> alert ("cache deletion failed! Please check whether the cached file exists "); return false ;}// define the cache file reminder function alert ($) {echo "script" alert ('$ a'); script ";}}?> Test. php
Cache_start (); // at the beginning of the page ------- the page outputs $ like = "I love oranges and bananas! "; Echo $ like ."
"; $ My_cache-> cache_end (); // final output
Writes data to the php buffer. Php output_buffering mechanism, meaning in tcp...