php buffer

不言
Release: 2023-03-23 06:18:01
Original
3142 people have browsed it

This article introduces the knowledge of PHP buffer. Now I share it with you and give a reference to friends who need help. Let’s take a look together.

What Is it a buffer?
Simply speaking, the function of a 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 The problem,

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

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 the output

When apache receives the instruction and just 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 PHP wants to output data time, 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 lead to certain errors, such as Cannot modify header information – headers already sent by;


The reason for this error is that some data has been output before the header, and while outputting this data, apache will send a response status 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 to 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.
And After the output data PHP opens the output buffer, the data will be stored in the buffer and wait for output. In this way, the errors that occurred before can be avoided.
2. When writing files and downloading programs through PHP.
In order to make file downloading safer and at the same time 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, so that
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 large, assume it is 100M , then without turning on the buffer output, all 100M data must be read out, and then returned to the page at once. If this is done, the user will not get a response until all the data has been read
, which reduces Improve the user experience.
If the output buffer is turned on, when the PHP program finishes reading a certain section of the file, it will immediately output it to apache, and then let apache return to the browser immediately, which can reduce the user's waiting time. Then what follows What to do with the data? We can write a while loop to read the file section by section.
Every time a section is read, it will be output immediately until the entire file is output, so that the browser can continue to receive data, and There is no need 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, all 100M files need to be read. into the memory, and then output. However, what if the PHP program has a memory limit? In order to ensure the stability of the server, the administrator usually sets a limit on the PHP execution
memory (through the total memory_limit of php.ini, its default The 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. This time, we need to use the above
method to solve this problem. Only read a certain paragraph at a time, so as to avoid memory limitations
3. Static file cache
Many companies now have such a The requirement is that 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 the next time it is visited, it will be displayed directly. Display the files saved on the server directly without operating through PHP
This is the so-called "static page cache". How can we return the content to the browser and save the data to the server at the same time? What about? This requires the use of the output buffer.

1

2


##echo "this is test ";

header("LOCATION http://www.baidu.com");


Configuration related to the output buffer

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

2.implicit_flush
This configuration directly affects the apache buffer, There are 2 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
Functions related to the buffer

1.ob_implicit_flush
The function is the same as implicit_flush, whether it is automatic Refresh apache's 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, regardless of the php.ini file How to configure, 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, you need to use Call the passed parameters to process the contents of the buffer once, and then put it into the buffer
4.ob_flush
Instructs 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 obtaining 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 content in the buffer and send the content to apache
11.ob_get_length
Get the length of the content in the buffer
12.ob_list_handlers
Get when running ob_start, The name of the function called back, 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 ob_start The callback parameter, before the buffer is refreshed, will call this function to perform gzip or deflate compression on the data. This function requires the support of zlib extension.

Related content of using the buffer
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 them first. ob_flush,
Execute flush again, because we need to send the data from PHP to apache first, and then return it from apache to the browser. If php has not refreshed the data to apache, and flush is called, apache will have nothing to do. The data is returned to the browser.

2. Some browsers will not display the data if they receive too few characters, such as the old version of IE (it must be larger than 256k to display). In this way This will cause a question. It is clear that the buffer is refreshed in both php and apache, but the browser does not appear the data you want. Maybe this is the reason. So when testing, you can output the data Add multiple spaces after it to fill in the data to ensure that the browser will not cause such weird problems.

3. Some webservers have some restrictions on their own output buffers, such as nginx. There is a configuration fastcgi_buffer_size 4k, which means that it will be refreshed when the content of its own output buffer reaches 4K, so in order to ensure the data of the content, you can add the following code to ensure the content length

1

2

3

4

5

6


##ob_start();

echo 'aaa';

$string = ob_get_contents();

file_put_contents('a.html', $string);

ob_flush();

flush();

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 fail to refresh. The reason is that mod_gzip has its own output buffer. When PHP executes flush Function, instructs apache to refresh the output buffer, but if 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, and it will not output immediately, so the content cannot be output immediately. In order to improve In this case, 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

#Related recommendations:


Detailed explanation to speed up your site by flushing the PHP buffer

What is the PHP buffer?

The above is the detailed content of php buffer. For more information, please follow other related articles on the PHP Chinese website!

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