PHP output buffer_PHP tutorial

WBOY
Release: 2016-07-13 10:10:06
Original
695 people have browsed it

PHP’s output buffer

What is a buffer?

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.).

When PHP is executed, where is the buffer used?

If you want to understand PHP's buffer, you need to know where the buffer is set when executing PHP.
When executing PHP, if you encounter code that outputs data such as echo print_r, PHP will put the data to be output into PHP's own buffer and wait for output.
When PHP's own buffer receives an instruction to output the contents of the buffer, it will output the data in the buffer to apache. Apache receives the data output by PHP and then stores the data in apache itself. In the buffer, wait until output
When apache receives an instruction and only wants to output the contents of the buffer, it will output the contents of the buffer and return it to the browser.

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.

What role does the buffer play in PHP?

1. The most common thing is that some data has been output before using the header function, which will cause certain errors, such as Cannot modify header information – headers already sent by;

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();

Configuration related to output buffer

In PHP.INI, there are two configuration items closely related to the buffer
1.output_buffering
This configuration directly affects the buffer of PHP itself. There are three configuration parameters. on/off/xK (x is an integer value);
on - turn on the buffer
off - Turn off the buffer
256k - Turn on the buffer, and when the content of the buffer exceeds 256k, the buffer will be automatically refreshed (send the data to apache);

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

Buffer-related functions

1.ob_implicit_flush
The function is the same as implicit_flush, whether to automatically refresh the apache buffer
2.flush
The function is to send instructions to apache and let apache refresh its own output buffer.
3.ob_start
Open the output buffer. No matter how the php.ini file is configured, if you use this function, even if output_buffering is set to off, the output buffer will be opened
The ob_start function also accepts a parameter, which is a callback of a function, which means that before inputting the buffer content, the buffer content needs to be processed once using the parameters passed in the call, and then placed into the buffer
4.ob_flush
Instruct php itself to refresh its own buffer and send the data to apache
5.ob_clean
Clear the contents of the php buffer
6.ob_end_clean
Clear the contents of the php buffer and close the output buffer
7.ob_end_flush
Send the contents of php's own buffer to apache, and clear the contents of its own buffer
8.ob_get_clean
After getting the contents of the buffer, clear the buffer.
9.ob_get_contents
Get the contents of the output buffer
10.ob_get_flush
Get the contents of the buffer and send them to apache
11.ob_get_length
Get the length of the content in the buffer
12.ob_list_handlers
Get the name of the function called back 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 of this function is as the callback parameter of ob_start. Before the buffer is refreshed, this function will be called to perform gzip or deflate compression on the data. This function requires the support of zlib extension.

Related content about using buffers

1. The order relationship between ob_flush and flush. From the above analysis, we can see that ob_flush is related to PHP itself, and flush operates the apache buffer. So when we use these two functions, we need to execute ob_flush first.
Then execute flush, because we need to send the data from PHP to apache first, and then return it to the browser from apache. If php has not refreshed the data to apache and calls flush, apache will not return any data to the browser. .

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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/938945.htmlTechArticlePHP’s output buffer What is a buffer? Simply put, the function of a buffer is to transfer input or output The content is first put into the memory without being displayed or read. As for why there is a buffer...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template