PHP output buffer _ PHP Tutorial

Source: Internet
Author: User
PHP output buffer. What is the buffer in the PHP output buffer? In short, the buffer is used to put the input or output content into the memory without displaying or reading it.
What is a buffer?

In short, the buffer is used to put the input or output content into the memory without displaying or reading the content. as to why there is a buffer zone, this is a wide range of questions. if you are interested, you can find the relevant information in Wangshan.
In fact, the most essential role of the buffer zone is to coordinate the operation of high-speed CPU and relatively slow IO devices (disks, etc.

Where can PHP be used to buffer during execution?To understand the PHP buffer zone, you need to know where the buffer zone is set during PHP execution.
When executing PHP, if you encounter code such as echo print_r that will output data, PHP will put the data to be output in the PHP buffer and wait for the output.
When the buffer zone of PHP receives a command to output the content of the buffer zone, the data in the buffer zone is output to apache, and apache receives the data output from PHP, then, store the data in the apache buffer and wait for the output.
When apache receives commands and only outputs the buffer content, it will output the buffer content to the browser.

It can be seen that when PHP wants to output data, it will go through two buffers (first its own, then apache's) and then return to the browser.

What role does the buffer play in PHP?1. the most common one is that some data has been output before the header function is used, which may cause some errors, such as Cannot modify header information-headers already sent;

12 echo "this is test";header("LOCATIONhttp://www.baidu.com");

The cause of this error is that some data has been output before the header, while outputting the data, apache will send a response to the browser at the same time (since there is output, that is, this request is valid), and then you use the header function again.
When an http header is sent, this error is returned. the error indicates that the HTTP header has been sent and cannot be modified.
Why can this error be avoided by using a buffer?
Because the header function is not affected by the buffer, when a header function is encountered, PHP immediately executes apache to send this http header to all browsers.
After the output data PHP opens the output buffer, the data will be stored in the buffer, waiting for the output. in this way, the previous errors can be avoided.
2. when writing a file downloading program through PHP.
To make file downloads safer and more controllable, many friends prefer to use PHP to write file download pages. the principle is very simple. it is to read and display the file content through fwrite, and then send the HTTP header through the header to let the browser know that this is an attachment.
To provide download results.
If the above method is used to provide the download page, a efficiency problem will occur. if a large file is assumed to be 100 MB, when the buffer output is not enabled, you must read all the M data and then return it to the page at a time. if you do so, all the data will be read.
Then the response is returned, reducing the user experience.
If the output buffer is enabled, when the PHP program reads a certain part of the file, it immediately outputs the file to apache, and then enables apache to return immediately to the browser, which can reduce user waiting time. what about the subsequent data? We can write a while loop to read the file for a while.
Each read segment is output immediately until all files are output, so that the browser can continue to receive data without waiting until all files are read.

In addition, this solution solves another very serious problem. for example, if a file is 100 MB and the buffer is not enabled, you need to read all the MB files into the memory and then output them. but what if the PHP program has a memory limit? To ensure server stability, administrators usually execute PHP
Memory sets a limit (through php. the default value of ini's total memory_limit is 8 MB. that is, the memory used by each PHP program cannot exceed this value. assume that the value is 8 M, and the file to be read is 100 M, there is no enough memory to read the file. at this time, we need to use the above
To solve this problem, read-only a segment each time, so as to avoid memory restrictions
3. static file cache
Many companies now have such a requirement that a page will execute PHP for the first visit and then return the displayed content to the browser, at the same time, you need to save the displayed content to the server, so that the files saved on the server will be directly displayed at the next visit, without the need for PHP operations.
This is the so-called "static page cache". how can we save the data to the server while returning the content to the browser? This requires the output buffer.

123456 ob_start();echo 'aaa';$string = ob_get_contents();file_put_contents('a.html',$string);ob_flush();flush();

Configurations related to the output bufferIn PHP. INI, there are two configuration items closely related to the buffer.
1. output_buffering
This configuration directly affects the buffer of php. There are three configuration parameters:. on/off/xK (x is an integer value );
On-enable the buffer
Off-disable the buffer
256 k-enable the buffer, and when the buffer content exceeds k, the buffer is automatically refreshed (data is sent to apache );

2. implicit_flush
This configuration directly affects the apache buffer. There are two configuration parameters:. on/off.
On-automatically refresh the apache buffer, that is, when php sends data to the apache buffer, it directly returns the output to the browser without waiting for other commands.
Off-does not automatically refresh the apache buffer. after receiving the data, wait for the refresh command

Buffer-related functions1. ob_implicit_flush
Same as implicit_flush, whether to automatically refresh apache buffer zone
2. flush
The role is to send commands to apache, so that apache can refresh its output buffer.
3. ob_start
Open the output buffer, regardless of how the php. ini file is configured. if this 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 of a function. it means that before the buffer content is input, the buffer content needs to be processed once by calling the passed parameter, into the buffer zone.
4. ob_flush
Instruct php to refresh its own buffer and send data to apache
5. ob_clean
Clear content in the php buffer
6. ob_end_clean
Clear the content in the php buffer and disable the output buffer.
7. ob_end_flush
Sends the content in the php buffer to apache and clears the content in the buffer.
8. ob_get_clean
After obtaining the buffer content, clear the buffer.
9. ob_get_contents
Obtain the content in the output buffer.
10. ob_get_flush
Obtain the content in the buffer and send the content to apache
11. ob_get_length
Obtain the length of the content in the buffer.
12. ob_list_handlers
Obtain the name of the callback function when running ob_start, for example:
Ob_start ('OB _ gzhandler ');
Print_r (ob_list_handlers );
Will print out ob_gzhandler;
13. ob_gzhandler
This function serves as the callback parameter of ob_start. before the buffer is refreshed, it will call this function to perform gzip or deflate compression on the data. this function requires zlib extension support.

Buffer-related content1. the order relationship between ob_flush and flush. the above analysis shows that ob_flush is related to php itself, and flush operates on the apache buffer. when using these two functions, we need to execute ob_flush first,
Then execute flush, because we need to first send the data from PHP to apache, and then the apache will return to the browser. if php has not refreshed the data to apache and flush is called, apache returns no data to the browser.

2. some browsers do not display data if they receive too few characters, for example, the old version of IE (must be larger than k ). this will cause a doubt that php and apache both refresh the buffer, but the browser does not display the desired data, which may be the cause. therefore, when testing, you can add multiple spaces behind the output data to fill up the data, so that the browser will not cause such strange problems.

3. some webservers have some restrictions on their own output buffer. for example, if nginx is configured with fastcgi_buffer_size 4 k, it indicates that when the content of its output buffer reaches 4 k, it will be refreshed, to ensure the data of the content, you can add the following code to ensure the content length.

12345 echo str_repeat(" ",4096); ?>

4. in apache, if you enable the mod_gzip compression module, the flush function may fail to be refreshed. The reason is that mod_gzip has its own output buffer, when php executes the flush function, it instructs apache to refresh the output buffer, but the content needs to be compressed. apache then outputs the content to its mod_gzip module. mod_gzip also has its own output buffer, he will not output the content immediately, so the content cannot be output immediately. to improve this situation, you can disable the mod_gzip module or. add the following content to conf to disable compression.

1 SetEnv no-gzip dont-vary

What is a buffer? In short, the buffer is used to put the input or output content into the memory without displaying or reading it. as for why there is a 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.