PHP output cache ob series functions. Basic principle of ob: If the ob cache is enabled, the echo data is first stored in the ob cache. For header information, it is directly stored in the program cache. When the page is executed to the end, the basic principle of the ob cached data ob is: if the ob cache is enabled, the echo data is first stored in the ob cache. For header information, it is directly stored in the program cache. After the page is executed, the ob cached data is put into the program cache and then returned to the browser in turn.
Next I will talk about the basic functions of ob:
1) prevent errors caused by the function that sends header files using setcookie (), header (), or session_start () after the browser outputs. In fact, it is better to use less and develop good code habits.
2) capture the output of some unrecoverable functions. for example, phpinfo () outputs a lot of HTML, but we cannot use a variable such as $ info = phpinfo (); to capture, at this time, ob will work.
3) process the output content, such as gzip compression, simple conversion, and replacement of some strings.
4) generating static files is actually capturing the output of the entire page and saving it as a file. It is often used in HTML generation or full-page cache.
For GZIP compression in the third point just mentioned, it may be something that many people want to use, but it is not actually used. In fact, you can implement gzip compression on the page by slightly modifying the code.
The code is as follows:
Ob_start (ob_gzhandler );
Content to be cached
Yes, you can add the ob_gzhandler callback function. However, there are some minor issues. One is that zlib is required, and the other is that the browser does not determine whether gzip is supported. (currently, it seems that all of them are supported, the iphone browser seems to be supported ).
In the past, we used to determine whether the browser supports gzip, use a third-party gzip function to compress the content of ob_get_contents (), and finally echo the content.
I. collection of common functions in ob functions
The code is as follows:
Ob_start (); // open an output buffer. all output information is not directly sent to the browser, but saved in the output buffer.
Ob_clean (); // delete the content of the internal buffer, do not close the buffer (do not output ).
Ob_end_clean (); // delete the content of the internal buffer and disable the buffer (no output ).
Ob_get_clean (); // returns the content of the internal buffer and closes the buffer. It is equivalent to executing ob_get_contents () and ob_end_clean ()
Ob_flush (); // sends the content of the internal buffer to the browser, deletes the content of the buffer, and does not close the buffer.
Ob_end_flush (); // sends the content of the internal buffer to the browser, deletes the content of the buffer, and closes the buffer.
Ob_get_flush (); // returns the content of the internal buffer, closes the buffer, and releases the content of the buffer. It is equivalent to ob_end_flush () and the buffer content is returned.
Flush (); // output all content released by ob_flush and content not in the PHP buffer to the browser; refresh the content in the internal buffer and output it.
Ob_get_contents (); // return the content of the buffer, not 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 mechanic.
Ob_get_status (); // Get status of output buffers.
Ob_implicit_flush (); // enable or disable the absolute refresh function. the default value is disabled. after the function is enabled, ob_implicit_flush (true) indicates that an absolute refresh is triggered when an output statement (e. g: echo) when executed, the output is directly sent to the browser, instead of calling flush () or waiting until the script ends.
Ob_gzhandler // The ob_start callback function, which uses gzip to compress the buffer content.
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, the output control is used in all scripts. if this value is a number, it indicates the maximum byte limit of the buffer, when the cached content reaches the upper limit, the content in the current buffer zone will be automatically output to the browser.
Output_handler // this option redirects all the output of the script to a function. For example, when output_handler is set to mb_output_handler (), the character encoding is changed to the specified encoding. Any processing function set will automatically process the output buffer.
Implicit_flush // the same action as ob_implicit_flush. the default value is Off.
II. Examples
1. enable the 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 header information sent using header () or setcookie (). it only applies to data blocks similar to echo () and PHP code.
The code is as follows:
Ob_start (); // open the buffer
Echo "Hello \ n"; // output
Header ("location: index. php"); // redirects the browser to index. php.
Ob_end_flush (); // output all content to the browser
Anyone who knows about the header () function knows that this function will send a file header to the browser, but if any output (including null output, for example, a blank space, carriage return, or line feed will prompt an error. If we remove ob_start () from the first line and execute this program, we will find an error message: "Header had all ready send "! However, when ob_start is added, no error will be prompted because when the buffer zone is opened, the characters after echo will not be output to the browser, but will be retained on the server until you use flush or ob_end_flush, so there will be no file header output errors!
2. Save the output of the phpinfo () function.
The code is as follows:
Ob_start (); // open the buffer
Phpinfo (); // use the phpinfo function
$ Info = ob_get_contents (); // Obtain the buffer content and assign it to $ info
$ File = fopen('info.txt ', 'w'); // open the info.txt file.
Fwrite ($ file, $ info); // write the information to info.txt
Fclose ($ file); // Close the info.txt file
3. static template technology
The so-called static template technology is to make the user obtain the html page generated by PHP on the client side in some way. If this html page is no longer updated, 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, sohu. The benefits of similar technologies are enormous.
The code is as follows:
Ob_start (); // open the buffer
All php page output
$ Content = ob_get_contents (); // retrieves all the content output on the php page.
$ Fp = fopen ("output00001.html", "w"); // create a file, open it, and write
Fwrite ($ fp, $ content); // write the content of the PHP page into output00001.html, and then ......
Fclose ($ fp );
III. output cache handle ob_gzhandler
PHP4.0.4 has a new output cache handle, ob_gzhandler, which is similar to the previous class but has different usage. When using ob_gzhandler, add the following content to php. ini:
The code is as follows:
Output_handler = ob_gzhandler;
This line of code enables PHP to activate the output cache and compress all the content it sends.
If you do not want to add this line of code in php. ini for some reason, 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:
The code is as follows:
Php_value output_handler ob_gzhandler
Or you can call it from the PHP code as follows:
The code is as follows:
Ob_start ("ob_gzhandler ");
The method of using the output cache handle is indeed very effective and does not impose any special load on the server. However, it must be noted that Netscape Communicator does not provide good support for compressed images. Therefore, unless you can ensure that all users use IE browsers, you should disable JPEG and GIF image compression. Generally, this compression is effective for all other files, but we recommend that you perform tests on different browsers, this is especially important when you use a special plug-in or data viewer.
Note:
1. the output_buffering of some Web servers is 4069 characters or larger by default, that is, the output content must reach 4069 characters before the server will flush refresh the output buffer. to ensure that the flush is effective, it is best to use the ob_flush () the following statements are available before the function:
The code is as follows:
Print str_repeat ("", 4096); // to ensure that the output_buffering value is reached
2. ob _ * series functions operate the output buffer of PHP, so ob_flush only refreshes the buffer of PHP and flush is the buffer of apache. Therefore, the correct sequence of use is: first ob_flush, then flush. Ob_flush is to release data from the PHP buffer, and flush is to send all the data inside/outside the buffer to the browser.
3. do not mistakenly believe that the echo/print output of the script will never be displayed in the browser after ob_start () is used. Because after the PHP script is run, the buffer is automatically refreshed and the content is output.
Bytes. For header information, it is directly stored in the program cache. When the page is executed to the end, the data cached by ob will be...