The main content of this article is a detailed explanation of refreshing the output buffer in PHP. It has a certain reference value. Now I share it with you. Friends in need can refer to it.
Buffer is a memory address space ,The default size of Linux system is generally 4096 (1kb), which is one memory page. It is mainly used to store data transfer areas between devices with unsynchronized speeds or devices with different priorities. Through the buffer, the processes can wait less for each other. Here is a more general example. When you open a text editor to edit a file, every time you enter a character, the operating system will not immediately write the character directly to the disk, but first write it to the buffer. When writing When a buffer is full, the data in the buffer will be written to the disk. Of course, when the kernel function flush() is called, it is mandatory to write the dirty data in the buffer back to the disk.
By the same token, in PHP, when echo and print are executed, the output is not immediately transmitted to the client browser for display 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 will hand over the output data in the php buffer to the system kernel and pass it to the browser via TCP for display. Therefore, the data will be written to these places in sequence echo/pring -> php buffer -> tcp buffer -> browser
There are three functions related to refreshing the buffer in PHP One:
1). flush
Refresh the buffer of the PHP program, regardless of the circumstances under which PHP is executed. This function sends all the program's output so far to the user's browser. But this function will not have any impact on the caching mode of the server or client browser, nor will it have any impact on the caching of PHP itself.
2).ob_flush
This function outputs the cache of PHP itself. PHP's own caching is controlled by output_buffering in php.ini. The function of ob_flush() is to take out the content originally stored in the output cache and set it to the waiting output state, but it will not be sent directly to the client. In this case, you need to use ob_flush() first and then flush(), the client to get the output of the script immediately.
The two PHP configurations related to PHP's own output buffering are:
Parameter 1: output_buffering: on/off or integer. When set to on, the output cache control will be used in all scripts without limiting the size of the cache. When set to an integer, such as output_buffering=4096, when the cache data reaches 4096 bytes, the cache will be automatically output and refreshed. The difference in this parameter is the reason why the above code has different execution results at different times. When output_buffering is turned off, all output (echo) of the script will be sent to the client immediately. When the above code is executed, a number will be output every second. After output_buffering is turned on, the output content will be cached on the server first, and will not be sent to the client until the end of the script.
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 sent to the browser immediately whenever there is output. In this way, there is no need to use flush() to send it to the browser after each output (echo).
The following are examples:
##[php] view plain copy
##
<?php ob_end_clean(); echo str_pad(" ", 256); for ($i=100; $i>0; $i--) { echo $i, '<br/>'; flush(); sleep(1); } ?>
copy
##<?php
echo str_pad(" ", 256);
for ($i=100; $i>0; $i--) {
echo $i, '<br />';
ob_flush();
flush();
sleep(1);
}
?>
<?php ob_implicit_flush(true); echo str_pad(" ", 256); for ($i=100; $i>0; $i--) { echo $i, '<br />'; ob_flush(); sleep(1); } ?>
1). Some web server programs, especially web server programs under Win32, will still cache the output of the script until the end of the program before sending the results to the browser. Some Apache modules, such as mod_gzip, may perform output caching themselves, which will cause the results generated by the flush() function to not be sent to the client browser immediately. Even browsers cache received content before displaying it. For example, the Netscape browser caches content until it receives a line break or the beginning of an html tag, and does not display the entire table until it receives a tag. Some versions of Microsoft Internet Explorer will only begin to display the page after receiving 256 bytes, so some extra spaces must be sent for these browsers to display the page content.
2). The impact of PHP installation mode. The above method can be used directly when PHP is installed as an Apache module. If you use FastCgi, you need to pay attention to the following configurations:
a). Apache Fcgid PHP
FcgidOutputBufferSize 0 (default is 65536)
When configuring Fcgid, set this The item value is 0, and the above code can achieve the desired effect only when the buffer is refreshed.
b).IIS FastCgi PHP
ResponseBufferLimit=0
Modify this item under WINDOWS\system32\inetsrv\fcgiext.ini.
c).nginx php-fpm
fastcgi_buffer_size 4k;
##fastcgi_buffers 8 4k; fastcgi_busy_buffers_size 4k<br><br>
#gzip off;Related recommendations:
<a href="http://www.php.cn/php-weizijiaocheng-384170.html" target="_self"></a>
##
The above is the detailed content of Detailed explanation of refreshing output buffer in PHP. For more information, please follow other related articles on the PHP Chinese website!