This article mainly introduces the content is about the PHP refresh output buffer in detail, has a certain reference value, now share to everyone, the need for friends can refer to
Buffer is a memory address space, the Linux system default size is generally 4096 (1KB), that is, a memory page. It is primarily used to store data between devices that are out of sync or between devices with different priority levels. By using buffer, you can make the process less of a mutual wait. Here is a popular example, when you open a text editor to edit a file, when you enter a character, the operating system does not immediately write this character directly to the disk, but the first write to buffer, when the buffer is filled with a buffer, the data will be written to disk, Of course, when the kernel function flush () is called, it is mandatory to write the dirty data in buffer back to disk.
Similarly, in PHP, when executing echo,print, the output is not immediately displayed via TCP to the client browser, but instead writes the data to PHP buffer. The PHP output_buffering mechanism means that a new queue is established before the TCP buffer, and the data must pass through the queue. When a PHP buffer is full, the script process will send the output data from PHP buffer to the system kernel to be displayed by TCP to the browser. So, the data will be written to these places echo/pring. PHP Buffer---browser
There are three functions related to refreshing buffering in PHP:
1). Flush
Refreshes the PHP program's buffering, regardless of the circumstances in which PHP executes. This function sends all the output of the program so far to the user's browser. However, the function does not have any effect on the cache mode of the server or client browser, nor does it have any effect on the cache of PHP itself.
2). Ob_flush
This function outputs the cache of PHP itself. The cache of PHP itself is controlled by the output_buffering in php.ini. The function of Ob_flush () is to remove the contents of the output cache, set to wait for the output state, but not directly to the client, you need to use Ob_flush () and then flush () before the client can immediately get the output of the script.
The two PHP configurations associated with the output buffering of PHP itself are:
Parameter 1:output_buffering : On/Off or integer. When set to on, output cache control is used in all scripts and does not limit the size of the cache. When set to an integer, such as output_buffering=4096, a flush cache is automatically output when the cached data reaches 4096 bytes. The difference between this parameter is what causes the code to perform differently at different times. When Output_buffering is off, all the output (echo) of the script is instantly sent to the client, and the code that executes it is the output of a number per second. When Output_buffering is turned on, the output is deferred to the server until the end of the script is sent to the client.
Parameter 2:implicit_flush: On/off. Setting on means that when the script has output, it is automatically sent to the client immediately. Equivalent to automatically flush () after Echo.
3). Ob_implicit_flush
This function forces the output to be sent to the browser whenever there is an output. This does not require each output (echo) to be sent to the browser with flush ().
Here are some examples:
[PHP] view plain copy
<?php Ob_end_clean (); echo Str_pad ("", "N"); for ($i =100; $i >0; $i-) { echo $i, ' <br/> '; Flush (); Sleep (1); } ? >
The above code should output $i once every second. The purpose of the above Echo Str_pad ("", 256) is that IE needs to accept 256 bytes before it begins to display. The above code also has the following two kinds of wording.
[PHP] view plain copy
<?php Echo Str_pad ("", n); for ($i =100; $i >0; $i-) { echo $i, ' <br/> '; Ob_flush (); Flush (); Sleep (1); } ? >
[PHP] view plain copy
<?php Ob_implicit_flush (true); echo Str_pad ("", "N"); for ($i =100; $i >0; $i-) { echo $i, ' <br/> '; Ob_flush (); Sleep (1); } ? >
In addition, we need to note that the refresh buffer is not only affected by the above, but also by the following effects:
1). Individual 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 make their own output caches, which will result in the flush () function producing results that are not immediately sent to the client browser. Even the browser will cache the received content before it is displayed. For example, the Netscape browser caches content before it accepts the start of a newline or HTML tag, and does not display the entire table until the </table> tag is accepted. Some versions of Microsoft Internet Explorer do not start to display the page until 256 bytes are accepted, so you must send some extra spaces to let these browsers display the page content.
2). The effect of PHP installation mode. The above method can be used directly in the case of PHP installed in Apache module mode. If you need to pay attention to the following configuration in the fastcgi way:
a). apache+fcgid+php
Fcgidoutputbuffersize 0 (default is 65536)
When you configure Fcgid, this value is set to 0, and the above code does the refresh buffer to achieve its desired effect.
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
gzip off;
相关推荐:
详解通过刷新PHP缓冲区为你的站点加速