PHP refresh output buffer details

Source: Internet
Author: User
Buffer is a memory address space. The default size of a Linux system is generally 4096 (1 kb), that is, a memory page. It is mainly used for data transmission between devices with Different Storage speeds or devices with different priorities. Through the buffer, the mutual waits of processes can be reduced. Here is a simple example. You can open a text editor to edit a file.

Buffer is a memory address space. The default size of a Linux system is generally 4096 (1 kb), that is, a memory page. It is mainly used for data transmission between devices with Different Storage speeds or devices with different priorities. Through the buffer, the mutual waits of processes can be reduced. Here is a simple example. You can open a text editor to edit a file.

Buffer is a memory address space. The default size of a Linux system is generally 4096 (1 kb), that is, a memory page. It is mainly used for data transmission between devices with Different Storage speeds or devices with different priorities. Through the buffer, the mutual waits of processes can be reduced. Here is a simple example. When you open a text editor and edit a file, the operating system does not immediately write this character to the disk for each input, instead, it is written to the buffer first. When the buffer is full, the data in the buffer is written to the disk. Of course, when the kernel function flush () is called, the dirty data in the buffer must be written back to the disk.

Similarly, in PHP, when echo and print are executed, the output is not immediately transmitted to the client browser through tcp, but 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/off or an 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 reaches 4096 bytes, the cache will be 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.

The following is an example:

 0; $i--) {   echo $i, '
'; flush(); sleep(1); } ?>
The above code should output $ I. echo str_pad ("", 256) every second to enable IE to display only after receiving 256 bytes. The above code is written in either of the following ways.

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

 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 results produced by the function are 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.

2). Influence of PHP installation mode. The above method can be used directly when PHP is installed using the Apache module. Pay attention to the following configurations if FastCgi is used:

A). Apache + Fcgid + PHP
FcgidOutputBufferSize 0 (65536 by default)

When configuring Fcgid, set this value to 0. The above code can achieve the desired effect only when refreshing the buffer.

B). IIS + FastCgi + PHP

ResponseBufferLimit = 0

Modify this item in WINDOWS \ system32 \ inetsrv \ fcgiext. ini.

C). nginx + php-fpm

fastcgi_buffer_size 4k;

fastcgi_buffers 8 4k;
fastcgi_busy_buffers_size 4k

gzip off;

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.