PHP output Buffer (buffering)

Source: Internet
Author: User

What is a buffer?
Simply put, the function of the buffer is to put the input or output content into the memory, not display or read. As to why there is a buffer, this is a very broad problem ~ 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 exported to Apache, Apache accepts the data from PHP, and then the data is present in the Apache buffer area, waiting for output, when Apache receives the instruction, Just to output the contents of the buffer, the contents of the buffer will be output and returned 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;

echo "This was 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 again use the header function to send the HTTP header , the error is returned, which means that the HTTP header has been sent out and you cannot modify it.
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 to the browser.
When PHP opens the output buffer, these output data will be stored in the buffer, waiting for the output. This avoids the errors that occurred earlier.
2 . When you write a file in PHP, download the program.
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, let the browser know that this is an attachment, This gives you the effect of downloading.
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 typically set a limit on PHP's execution memory (by php.ini total Memory_limit, which defaults to 8M), which means that the memory used by each PHP program cannot use more than this value. Assuming the value is 8M and the file to be read is 100M, there is simply not enough memory to read the file. This time, we need to use the above method to solve this problem, each time only read a paragraph, so that the memory limit can be avoided.
3. static file Caching
Now a lot of companies have a requirement, that 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 that the next time you visit, the file is stored directly on the server directly displayed, Without having to do it with PHP.
This is called "Static page Caching". So how do you get the content back to the browser and save the data to the server? This will use the output buffer.

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, there are 2 configuration parameters. On/off
on   -automatically refreshes the Apache buffer, i.e., When PHP sends data to Apache's buffer, it does not need to wait for other instructions, directly return the output to the browser
off   -do not automatically refresh the Apache buffer, after receiving the data, waiting for the refresh instructions

buffer-related functions
1.ob_implicit_flush
Role and Implicit_flush, whether to automatically refresh Apache buffers
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
Ob_start function also accepts a parameter, This parameter is a callback of a function, which means that the contents of the buffer need to be processed once using the arguments passed in by the call, and then placed in the buffer
4.ob_flush
instructs PHP itself to flush its own buffer, sending 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 the buffer in PHP itself to Apache and clears the buffer after clearing the contents of its buffer from
8.ob_get_clean
to get the contents of the buffer.
9.ob_get_contents
Gets the contents of the output buffer
10.ob_get_flush
Gets the contents of the buffer and sends the contents to Apache
11.ob_get_length
Gets the length of the content in the buffer
12.ob_list_handlers
Gets the function name of the callback when running Ob_start, for example:
Ob_start (' Ob_gzhandler ');
Print_r (ob_list_handlers); The
will print out the Ob_gzhandler;
13.ob_gzhandler
The function acts as a callback parameter to the Ob_start, which is called before the buffer is refreshed to perform gzip or deflate compression of the data. This function requires support for zlib extensions.

working with buffer related content
1. Sequence relationship of Ob_flush and flush. The above analysis shows that Ob_flush is related to PHP itself, while the flush operation is the Apache buffer, all of which we need to execute first when we use these two functions Ob_flush ,
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 you want, perhaps this is the cause. So when testing, you can add more than one space after the output data to fill the data, to determine that the browser will not cause such a strange problem.

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 output buffer of its own content reached 4K will be refreshed, So in order to guarantee the content of the data, you can add the following code to ensure the content length

<?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 the Mod_gzip has its own output buffer, and when PHP executes the flush function, Instructs Apache to flush out the output buffer, but 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 caused the content can not be output immediately. To improve the situation, you can close the MoD _gzip module, or add the following in httpd.conf to suppress compression

SETENV No-gzip Dont-vary

< reference:http://www.keepmyway.com/index.php/124.html>

PHP output Buffer (buffering)

Related Article

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.