Detailed explanation of PHP output cache ob series functions_PHP tutorial

WBOY
Release: 2016-07-13 10:36:17
Original
816 people have browsed it

Basic principle of ob: If the ob cache is turned on, the echo data is first placed in the ob cache. If it is header information, it is placed directly in the program cache. When the page is executed to the end, the ob cached data will be placed in the program cache, and then returned to the browser in turn.
Let me talk about the basic functions of ob:
1) Prevent the use of functions such as setcookie(), header() or session_start() to send header files after the browser has output. error. In fact, it is better to use this kind of usage less often and develop good coding habits.
2) Capture the output of some unobtainable functions. For example, phpinfo() will output a lot of HTML, but we cannot use a variable such as $info=phpinfo(); to capture it. At this time, ob will be useful. .
3) Process the output content, such as gzip compression, conversion between Simplified and Traditional Chinese, and some string replacement.
4) Generating static files is actually capturing the output of the entire page and then saving it as a file. Often used in HTML generation or full page caching.

Regarding the GZIP compression mentioned in the third point just mentioned, many people may want to use it, but have not actually used it. In fact, by slightly modifying the code, you can achieve gzip compression of the page.

Copy code The code is as follows:
ob_start(ob_gzhandler);
Content to be cached

Yes, Just add a callback function called ob_gzhandler, but there are some minor problems with this. First, it requires zlib support, and second, it does not determine whether the browser supports gzip (it seems to support it now, and all iPhone browsers seem to support it).
The previous approach was to determine whether the browser supports gzip, then use the third-party gzip function to compress the content of ob_get_contents(), and finally echo.

1. Collection of commonly used functions in ob series functions

Copy code The code is as follows:

ob_start(); //Open an output buffer. All output information is no longer sent directly to the browser, but is saved in the output buffer.

ob_clean(); //Delete the contents of the internal buffer without closing the buffer (no output).
ob_end_clean(); //Delete the contents of the internal buffer and close the buffer (no output).
ob_get_clean(); //Return the contents of the internal buffer and close the buffer. Equivalent to executing ob_get_contents() and ob_end_clean()
ob_flush();                                                                                                ​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​sends the content of the buffer and deletes the content of the buffer without closing the buffer.
ob_end_flush(); //Send the contents of the internal buffer to the browser, delete the contents of the buffer, and close the buffer.
ob_get_flush(); //Return the contents of the internal buffer, close the buffer, and then release the contents of the buffer. Equivalent to ob_end_flush() and returns the buffer contents.
flush(); // Output the content released by ob_flush and the content not in the PHP buffer to the browser; refresh the content of the internal buffer and output it.

ob_get_contents(); //Return the contents of the buffer without output.
ob_get_length(); //Returns the length of the internal buffer. If the buffer is not activated, this function returns FALSE.
ob_get_level(); //Return the nesting level of the output buffering mechanism.
ob_get_status(); //Get status of output buffers.

ob_implicit_flush(); //Turn on or off absolute refresh. The default is off. After turning on ob_implicit_flush(true), the so-called absolute refresh means that when an output statement (e.g: echo) is executed, the output is sent directly to The browser no longer needs to call flush() or wait until the end of the script to output.

ob_gzhandler //ob_start callback function, uses gzip to compress the contents of the buffer.
ob_list_handlers //List all output handlers in use
output_add_rewrite_var //Add URL rewriter values
output_reset_rewrite_vars //Reset URL rewriter values

The behavior of these functions is affected by the php_ini settings:
output_buffering //When this value is ON, output control will be used in all scripts; if the value is a number, it represents the maximum byte limit of the buffer , when the cached content reaches the upper limit, the content in the current buffer will be automatically output to the browser.
output_handler //This option can redirect all output of the script to a function. For example, when output_handler is set to mb_output_handler(), the character's encoding will be modified to the specified encoding. Any processing functions set will automatically handle output buffering.
implicit_flush //The function is the same as ob_implicit_flush, the default is Off.

2. Examples

1. There can be echo code before the header() function
The Output Control function allows you to freely control the output of data in the script. It is very useful, especially when you want to output the file header after the data has been output.
The output control function does not affect the file header information sent using header() or setcookie(), only those data blocks similar to echo() and PHP code.

Copy code The code is as follows:
ob_start(); //Open the buffer
echo "Hellon"; //Output
header(“location:index.php”); //Redirect the browser to index.php
ob_end_flush(); //Output all content to the browser

Everyone who knows the header() function knows that this function will send a file header to the browser, but if there is any output before using this function (including empty output, such as spaces, carriage returns and line break) will prompt an error. If we remove ob_start() in the first line and then execute this program, we will find that we get an error message: "Header had all ready send by"! But with ob_start, there will be no error message. The reason is that when the buffer is opened, the characters after echo will not be output to the browser, but will be retained on the server. They will not be output until you use flush or ob_end_flush, so it will not Any file header output errors!
2. Save the output of the phpinfo() function
Copy the code The code is as follows:
ob_start() ; ; .txt', 'w'); //Open the file info.txt
fwrite($file, $info); //Write information to info.txt
fclose($file); //Close the file info.txt


3. Static template technology

The so-called static template technology is to use a certain method to enable users to get the html page generated by PHP on the client side. If this HTML page will no longer be updated, then when another user browses this page again, the program will no longer call PHP and related databases. For some websites with a large amount of information, such as sina, 163, and sohu. The benefits of technology like this are huge.

Copy code
The code is as follows:ob_start(); content = ob_get_contents(); //Get all the content output by the php page $fp = fopen("output00001.html", "w"); //Create a file and open it for writing
fwrite ($fp, $content); //Write all the contents of the php page to output00001.html, and then...
fclose($fp);


3. Output cache handle ob_gzhandler

PHP4.0.4 has a new output cache handler ob_gzhandler, which is similar to the previous class, but its usage is different. The content to be added to php.ini when using ob_gzhandler is as follows:

Copy code

The code is as follows:
output_handler = ob_gzhandler; This line of code causes PHP to activate output caching and compress everything it sends out.
If for some reason you don’t want to add this line of code to php.ini, you can also change the default server behavior (not compressed) through the .htaccess file in the directory where the PHP source file is located. The syntax is as follows:

Copy code


The code is as follows:

php_value output_handler ob_gzhandlerOr call it from PHP code as follows:

Copy code

The code is as follows:

ob_start("ob_gzhandler");
The method of using the output cache handle is indeed very effective and does not bring any special load to the server. But it must be noted that Netscape Communicator has poor support for compressed graphics, so unless you can ensure that all users use IE browser, you should disable compressed JPEG and GIF graphics. In general, this compression works for all other files, but it is recommended that you test it separately for each browser, especially if you use special plug-ins or data viewers. This is especially important.
Notes:
1. The output_buffering of some web servers defaults to 4069 characters or larger, that is, the output content must reach 4069 characters before the server flushes the output buffer. In order to ensure that the flush is effective, It is best to have the following statement before the ob_flush() function:
Copy code The code is as follows:
print str_repeat("", 4096); //To ensure that the output_buffering value is reached

2. The ob_* series of functions operate the output buffer of PHP itself, so ob_flush only refreshes PHP's own buffer, while flush refreshes the apache buffer. Therefore, the correct order to use the two is: ob_flush first, then flush. ob_flush releases data from PHP's buffer, and flush sends all data in/out of the buffer to the browser.
3. Don’t mistakenly think that after using ob_start(), the script’s echo/print and other output will never be displayed in the browser. Because after the PHP script ends, the buffer will be automatically refreshed and the content will be output.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/740209.htmlTechArticleThe basic principle of ob: If the ob cache is turned on, the echo data is first placed in the ob cache. If it is header information, it is placed directly in the program cache. When the page is executed to the end, the data cached by ob will be...
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!