Detailed explanation of refreshing output buffer in PHP

不言
Release: 2023-03-24 16:44:02
Original
1592 people have browsed it

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 byoutput_bufferingin 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 plaincopy



##
0; $i--) { echo $i, '
'; flush(); sleep(1); } ?>
Copy after login
The above code should be output every one second $i. The purpose of the above echo str_pad(" ", 256) is that IE needs to receive 256 bytes before starting to display. The above code can also be written in the following two ways.





#[php]

view plain

copy



##

0; $i--) { echo $i, '
'; ob_flush(); flush(); sleep(1); } ?>
Copy after login



##[php ]

view plain
copy



##
0; $i--) { echo $i, '
'; ob_flush(); sleep(1); } ?>
Copy after login

    In addition, we also need to pay attention to the fact that the refresh buffer is not only affected by the above aspects, but also affected by the following:


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 theflush()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 atag. 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

#gzip off;Related recommendations:

Detailed explanation of how to speed up your site by flushing PHP buffers

##

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!