To put it simply, the function of the buffer is to put the input or output content into the memory first without displaying or reading it. As for why there is a buffer, this is a very broad question. If you are interested, You can find information on the website.
In fact, the most essential role of the buffer is to coordinate the operation of high-speed CPU and relatively slow IO devices (disks, etc.).
It can be seen that when PHP wants to output data, it will pass through two buffers (first its own, then apache's), and then return to the browser.
1 2 |
echo "this
is test" ;
header( "LOCATIONhttp://www.baidu.com" );
|
The reason for this error is that some data has been output before the header, and while outputting this data, apache will also send a response status to the browser (since there is output, the request is valid ), and then you use the header function again
If you send the HTTP header, this error will be returned. The error means: the HTTP header has been sent and you cannot modify it.
Why can using a buffer avoid this error?
Because the header function is not affected by the buffer, when it encounters the header function, PHP immediately executes apache to send this http header to the browser.
After the output data PHP opens the output buffer, the data will be stored in the buffer and wait for output. This can avoid the errors that occurred before.
2. When writing a file download program through PHP.
In order to make file downloading safer and improve more controllability, many friends like to use PHP to write file download pages. The principle is very simple, which is to read and display the file content through fwrite, and then send the HTTP header through header , let the browser know that this is an attachment, like this
This can achieve the effect of providing downloads.
If you use the above method to provide a download page, you will encounter an efficiency problem. If a file is very large, say 100M, then without turning on the buffer output, you must read out all 100M of data and then return to it at once. On the page, if you do this, the user will read all the data
The response will not be received until later, which reduces the user experience.
If the output buffer is enabled, when the PHP program finishes reading a certain section of the file, it will immediately output it to apache, and then let apache immediately return to the browser, which can reduce the user's waiting time. What about the subsequent data? We can Write a while loop to read the file piece by piece
Every time a paragraph is read, it is output immediately until all files are output, so that the browser can continue to receive data without having to wait until all files are read.
In addition, this approach also solves another very serious problem. For example, if a file is 100M, if the buffer is not turned on, the entire 100M file needs to be read into the memory and then output. However, if PHP Is there a memory limit for the program? In order to ensure the stability of the server, administrators usually execute PHP
Set a limit on memory (through the total memory_limit of php.ini, its default value is 8M), that is, the memory used by each PHP program cannot use more than this value. Assume that the value is 8M, and the file to be read is 100M, there is simply not enough memory to read the file. At this time, we need to use the above
The way to solve this problem is to only read a certain paragraph at a time, so as to avoid memory limitations
3. Static file caching
Many companies now have such a requirement, that is, when a certain page is visited for the first time, PHP will be executed, and then the displayed content will be returned to the browser. At the same time, the displayed content needs to be saved to the server, so that next time When accessing, the files saved on the server are directly displayed without the need to operate through PHP
This is the so-called "static page cache". So how can we return the content to the browser and save the data to the server at the same time? This requires the use of the output buffer.
1 2 3 4 5 6 |
ob_start();
echo 'aaa' ;
$string =
ob_get_contents();
file_put_contents ( 'a.html' , $string );
ob_flush();
flush ();
|
2.implicit_flush
This configuration directly affects apache's buffer and has two configuration parameters. on/off
on - Automatically refresh the apache buffer, that is, when PHP sends data to the apache buffer, it does not need to wait for other instructions and directly returns the output to the browser
off - Do not automatically refresh the apache buffer. After receiving the data, wait for the refresh command
2. Some browsers, if they receive too few characters, will not display the data, such as the old version of IE (it must be larger than 256k to display). This will cause a question, obviously in php Both the browser and apache have refreshed the buffer, but the browser does not appear the data it wants. This may be the reason. So when testing, you can add multiple spaces after the output data to fill in the data. Full data, make sure the browser will not cause such weird problems.
3. Some webservers have some restrictions on their own output buffers. For example, nginx has a configuration of fastcgi_buffer_size 4k, which means that it will not be refreshed until the content of its own output buffer reaches 4K, so in order to ensure For content data, you can add the following code to ensure the content length
1 2 3 4 5 |
<?php
echo str_repeat ( "
" ,4096);
?>
|
4. In apache, if you enable the compression module of mod_gzip, this may cause your flush function to be refreshed unsuccessfully. The reason is that mod_gzip has its own output buffer. When PHP executes the flush function, the instruction Apache refreshes the output buffer, but the content needs to be compressed. Apache outputs the content to its own mod_gzip module. mod_gzip also has its own output buffer, and it will not output immediately, so the content cannot be output immediately. In order to improve this situation, You can turn off the mod_gzip module, or add the following content to httpd.conf to disable compression
1 |
SetEnv no-gzip dont-vary
|