For some instructions on the header (' Location:url '), the PHP buffer

Source: Internet
Author: User
Tags sapi

What is a buffer?
Simply put, the function of the buffer is to put the input or output of the content into the memory, and not display or read. As to why there is a buffer, this is a very broad problem, if interested, you can find the information in the net Mountain.
In fact, the most essential role of the buffer is to coordinate the operation of high-speed CPUs and relatively slow IO devices (disks, etc.).

Where is PHP useful to buffer when it executes?
To understand the PHP buffer, it is necessary to know where the buffer is set when PHP is executed.
When PHP is executed, if it encounters the code of the output data such as Echo Print_r, PHP will put the output data into the buffer of PHP itself, waiting for the output.
When the buffer of PHP itself is instructed to output the contents of the buffer, the data in the buffer will be output to Apache, Apache receives the data from the PHP output, and then the data is present in the Apache buffer area until the output
When Apache receives the instruction, it simply outputs the contents of the buffer and returns the contents of the buffer to the browser.

This shows that when PHP is going to output data, it will go through two buffers (first of its own, then Apache) and back to the browser.

What role does the buffer play in PHP?
1. The most common is that some data has been output before using the header function, which results in some errors, such as cannot modify header information–headers already sent by;

12 echo   "This is test" header ( "location Http://www.baidu.com"

This error occurs because some data has been output before the header, and when the data is output, Apache will send a response state to the browser (since the output, that is, the request is valid), and then you use the header function again
Send HTTP headers, this error will be returned, the error means: HTTP header has been sent out, you can not modify him again.
Why use buffers to avoid this error?
because the header function is not affected by the buffer, when a header function is encountered, PHP immediately executes Apache send this HTTP header browser.
and the output data PHP opens the output buffer, the data will be stored in the buffer, waiting for output. This avoids the errors that occurred earlier.
2. When you write a file to download the program via PHP.
in order to make the file download more secure and more controllable, many friends like to use PHP to write files to download the page. The principle is simple, that is, through the fwrite to read and display the contents of the file, and then through the header to send HTTP headers, so that the browser know that this is an attachment, so that The
can achieve the effect of providing the download.
If you use the above method to provide download page, there will be an efficiency problem, if a file is large, assuming that 100M, then without opening the buffer output, you must read all the 100M data, and then return to the page at once, if you do so, the user will read all the data
And then get a response that reduces the user experience.
If the output buffer is turned on, when the PHP program reads a section of the file and then prints it out to Apache immediately, and then allows Apache to return to the browser immediately, this reduces the user wait time. What about the data behind that? We can write a while loop, Read the file for a period of time
reads it out at a time, until the file is fully output, so that the browser can continue to receive data without waiting for all files to be read.

In addition, this approach solves another serious problem. For example, a file is 100M, if the buffer is not turned on, you need to read all the 100M files into memory, and then output. But what if the PHP program makes a memory limit? In order to ensure the stability of the server, Administrators usually put PHP's execution
Memory set a limit (by PHP.ini total Memory_limit, the default value is 8M), that is, each PHP program uses memory can not use more than this value of memory. Assuming the value is 8M, and the file to be read is 100M, There is simply not enough memory to read the file. At this point, we need to use the
method above to solve this problem, each time read only one paragraph, so that the memory limit can be avoided
3. Static file caching
Now a lot of companies have such a demand, is a page in the first visit, will execute PHP, and then return the displayed content to the browser, but also need to save this display to the server, so the next time you visit, the file is saved directly on the server directly displayed, and do not need to do the operation of PHP
This is called "Static page Caching". So how do you get the content back to the browser while saving the data to the server? This will use the output buffer.

123456 < Code class= "PHP Plain" >ob_start (); echo   ' aaa ' ; $string  = ob_get_contents (); file_put_contents ( ' a.html ' $string ob_flush (); flush ();

Configuration related to output buffers
In php.ini, there are two configuration items that are closely related to a buffer
1.output_buffering
This configuration directly affects the buffer of PHP itself, there are 3 configuration parameters. ON/OFF/XK (x is an integer value);
On-Open buffer
Off-Close buffer
256k-The buffer is opened, and when the buffer content exceeds 256k, the buffer is automatically refreshed (sending the data to Apache);

2.implicit_flush
This configuration directly affects the Apache buffer, which has 2 configuration parameters. On/Off
On-automatically refreshes the Apache buffer, that is, when PHP sends data to the Apache buffer, there is no need to wait for other instructions, to directly return the output to the browser
Off-does not automatically flush Apache buffers, wait for refresh instructions after receiving data

Buffer-related functions

1.ob_implicit_flush
function and Implicit_flush, whether the Apache buffer is refreshed automatically
2.flush
The function is to send an instruction to Apache and let Apache flush its output buffers.
3.ob_start
Opens the output buffer, regardless of how the php.ini file is configured, and if the function is used, the output buffer is opened even if output_buffering is set to OFF
The Ob_start function also accepts a parameter, which is a callback for a function, which means that the contents of the buffer need to be processed once, and then placed in the buffer, before the contents of the input buffer are passed in.
4.ob_flush
Instructs PHP itself to flush its own buffer and send the data to Apache
5.ob_clean
Clears the contents of the PHP buffer
6.ob_end_clean
Clears the contents of the PHP buffer and closes the output buffer
7.ob_end_flush
Sends the contents of PHP's own buffer to Apache and clears the contents of its own buffer.
8.ob_get_clean
Clears the buffer after it gets the contents of the buffer.
9.ob_get_contents
Get the content in the output buffer
10.ob_get_flush
Gets the contents of the buffer and sends the content to Apache
11.ob_get_length
Gets the length of the content in the buffer
12.ob_list_handlers
Gets the name of the function that is being recalled when running Ob_start, for example:
Ob_start (' Ob_gzhandler ');
Print_r (ob_list_handlers);
will print out ob_gzhandler;
13.ob_gzhandler
The function acts as a callback parameter for the Ob_start, which is called before the buffer refreshes to either gzip or deflate the data. This function requires the support of the zlib extension.

Working with buffer related content
The order relationship of 1.ob_flush and flush. The above analysis shows that Ob_flush is related to PHP itself, while the flush operation is Apache buffer, all of which we need to execute ob_flush when we use these two functions.
Execute flush again, because we need to send the data from PHP to Apache and then back to the browser by Apache. If PHP has not yet flushed the data to Apache, it calls Flush, and Apache has no data to return to the browser.

2. Some browsers, if the number of characters received is too small, it will not show the data, such as the old version of IE (must be more than 256k to display). This will cause a doubt, obviously in PHP and Apache have done a flush buffer operation, but the browser does not appear the data they want, Maybe that's what this is all about. So when testing, you can add more than one space after the output data to fill up the data to make sure that the browser does not cause such strange problems.

3. Some webserver, his own output buffer will have some restrictions, such as nginx, he has a configuration fastcgi_buffer_size 4k, that is, when the content of its output buffer to 4K will be refreshed, so in order to guarantee the content of the data, You can add the following code to guarantee the content length

12345 <?phpecho str_repeat(" ",4096);?>

4. In Apache, if you turn on the Mod_gzip compression module, this may cause your flush function to be unsuccessful, because Mod_gzip has its own output buffer, and when PHP executes the flush function, it instructs Apache to flush the output buffer, However, the content needs to be compressed, Apache will output the content to its own Mod_gzip module, Mod_gzip also has its own output buffer, he will not immediately output, so that the content can not be output immediately. To improve the situation, you can close the Mod_gzip module, or add the following to httpd.conf to suppress compression

1 SetEnv no-gzip dont-vary
Original link: http://www.keepmyway.com/index.php/124.html

3 in-depth understanding of the difference between Ob_flush and flush

Ob_flush/flush's description in the manual, is to refresh the output buffer, and also need to be used, so it will cause many people confused ...

In fact, the two of them are different objects, in some cases, flush does not do anything at all.

The Ob_* series function is the output buffer that operates PHP itself.

So, Ob_flush is the buffer that refreshes PHP itself.

Flush, strictly speaking, this only works when PHP is installed as an Apache module (handler or filter). It is a buffer that refreshes the webserver (which can be considered specifically Apache).

Under Apache module's SAPI, flush will invoke Sapi_module's flush member function pointer indirectly, calling Apache's Api:ap_rflush to flush Apache's output buffer, and of course the manual says, There are some Apache other modules that may change the result of this action:

    1. Some Apache modules, such as Mod_gzip, may do their own output caching,
    2. This causes the flush () function to produce results that are not immediately sent to the client browser.
    3. Even the browser will cache the received content before it is displayed. such as Netscape
    4. The browser caches the content before it accepts the start of a newline or HTML tag, and
    5. The entire table is not displayed until the </table> tag is accepted.
    6. Some versions of Microsoft Internet Explorer are only accepted when the 256
    7. Byte to start displaying the page, so you must send some extra space to make this
    8. Some browsers display the contents of the page.

So, the correct use of the two is the order. First Ob_flush, then flush,

Of course, in other sapi, do not call flush also can, only to ensure that your code portability, recommended for use.

For some instructions on the header (' Location:url '), the PHP buffer

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.