OB series function output Cache usage

Source: Internet
Author: User
Tags file info phpinfo setcookie
OB, output buffer, is the abbreviation for the output buffering, not the output cache. OB is used to be able to help speed, but blind plus OB function, will only increase the CPU extra burden

The basic principle of OB: If the OB cache is open, then Echo's data is first placed in the OB cache. If it is header information, put it directly in the program cache. When the page executes to the end, the data of the OB cache is placed in the program cache and returned to the browser in turn.
Here is the basic role of OB:
1) Prevent errors caused by functions such as Setcookie (), header (), or session_start () to send header files after the browser has output. In fact, this usage is less good, develop good code habits.
2) Capturing the output of some unreachable functions, such as phpinfo () will output a whole bunch of HTML, but we can't use a variable such as $info=phpinfo () to capture it, and the OB works.
3) The content of the output is processed, such as gzip compression, for example, for simple conversion, for example, some string substitution.
4) Generate static files, in fact, capture the entire page of output, and then save as a file. Often used in generating HTML, or full-page caching.

For the 3rd in the gzip compression, may be a lot of people want to use, but there is no real use, in fact, slightly modify the code, you can achieve the gzip compression of the page.

Ob_start (Ob_gzhandler); content to cache

Yes, add a Ob_gzhandler This callback function is OK, but this is a little problem, one needs zlib support, two is not to determine whether the browser supports gzip (now seems to support, the iphone browser seems to support).
The previous practice was to determine if the browser supports gzip, and then use the third-party gzip function to compress the contents of the ob_get_contents (), and finally echo.

A collection of functions commonly used in OB series

Ob_start (); Open an output buffer, and all output information is not sent directly to the browser, but is stored in the output buffer.

Ob_clean (); Deletes 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 (not output).
Ob_get_clean (); Returns the contents of the internal buffer, closing the buffer. Equivalent to executing ob_get_contents () and Ob_end_clean ()
Ob_flush (); Sends the contents of the internal buffer to the browser, deleting the contents of the buffer and not closing the buffer.
Ob_end_flush (); Sends the contents of the internal buffer to the browser, deletes the contents of the buffer, and closes the buffer.
Ob_get_flush (); Returns the contents of the internal buffer, closes the buffer, and then releases the contents of the buffer. Equivalent to Ob_end_flush () and returns the buffer contents.
Flush (); Outputs the contents of the Ob_flush, as well as the content not in the PHP buffer, to the browser, refreshes the contents of the internal buffer, and outputs.

Ob_get_contents (); Returns the contents of the buffer, not output.
Ob_get_length (); Returns the length of the internal buffer, which returns false if the buffer is not activated.
Ob_get_level (); Return The nesting level of the output buffering mechanism.
Ob_get_status (); Get status of output buffers.

Ob_implicit_flush (); Turn absolute refresh on or off, default is off, turn on Ob_implicit_flush (true), the so-called absolute refresh, that is, when an output statement (E.g:echo) is executed, the output is sent directly to the browser, and no need to call flush () Or wait until the end of the script to output.

Ob_gzhandler the//ob_start callback function to compress the contents of the buffer with gzip.
Ob_list_handlers//list all output handlers on 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 setting:
Output_buffering//This value is on when output control is used in all scripts, and if the value is a number, it represents the maximum byte limit of the buffer, and when the cache content reaches that limit it will automatically output the contents of the current buffer to the browser.
Output_handler//This option redirects all output of the script to a function. For example, when Output_handler is set to Mb_output_handler (), the encoding of the character is modified to the specified encoding. Any processing function that is set will automatically process the output buffer.
Implicit_flush//function with Ob_implicit_flush, default is off.

Second, the example explanation

1. You can have the Echo code before the header () function
The output control function gives you the freedom to control the outputs of the data in your script. It is very useful, especially when you want to output a file header after the data has been output.
The output control function does not affect the header information sent using header () or Setcookie (), only data blocks that resemble Echo () and PHP code are useful.

Ob_start ();                   Open buffer  echo "hello\n";               Output  Header ("location:index.php");//redirect the browser to index.php   Ob_end_flush ();               Output all content to browser

All people who know about the header () function know that this function sends a file header to the browser, but if there are any outputs (including null output, such as spaces, carriage returns, and line feeds) before using this function, an error is indicated. If we get rid of the first line of Ob_start () and execute the program, we'll find an error message: "Header had all ready to send by"! But with Ob_start, there is no hint of error, because when the buffer is opened, the character behind the echo is not output to the browser, but remains on the server until you use flush or ob_end_flush to output, so there will be no file header output error!
2. Save the output of the phpinfo () function

Ob_start ();                      Open buffer   phpinfo ();                       Use the Phpinfo function   $info = ob_get_contents ();       The contents of the buffer area are obtained and assigned to $info   $file = fopen (' Info.txt ', ' W ');  Open File Info.txt   fwrite ($file, $info);            Write information to Info.txt   fclose ($file);                   Close File Info.txt

3. Static Template Technology
The so-called static template technology is in some way, so that users on the client side is generated by PHP HTML page. If this HTML page is not updated again, then when another user browses to the page again, the program will no longer invoke PHP and the associated database, for some of the more informative sites, such as Sina, 163, Sohu. The benefits of a technology like this are enormous.

Ob_start ();                            Open buffer full output of PHP page   $content = ob_get_contents ();          Get the full contents of the PHP page output   $fp = fopen ("output00001.html", "w");  Create a file and open, ready to write   fwrite ($fp, $content);                 Write the contents of the PHP page to output00001.html, then ...   Fclose ($FP);

Third, output cache handle Ob_gzhandler
PHP4.0.4 has a new output cache handle Ob_gzhandler, similar to the previous class, but with a different usage. The content to be added to PHP.ini when using Ob_gzhandler is as follows:

Output_handler = Ob_gzhandler;

This line of code allows PHP to activate the output cache and compress everything it sends out.

If for some reason you do not want to add this line of code to php.ini, you can also change the default server behavior (not compressed) by using the. htaccess file in the directory where the PHP source files are located, with the following syntax:

Php_value Output_handler Ob_gzhandler

Or it is called from the PHP code, as follows:

Ob_start ("Ob_gzhandler");

The method of using output cache handles is really effective and does not give the server any special load. It is important to note, however, that Netscape Communicator has poor support for compressed graphics, so you should suppress JPEG and GIF graphics unless you can ensure that all users are using Internet Explorer. Generally, this compression works for all other files, but it is recommended that you test separately for various browsers, especially if you are using a special plug-in or data viewer.
Precautions:
1, some Web server output_buffering default is 4069 characters or larger, that is, the output must reach 4069 character server to flush the output buffer, in order to ensure that flush is valid, it is best to have the following statement before the Ob_flush () function:

Print Str_repeat ("", 4096);  To ensure that the output_buffering value is reached

2, Ob_* series function is to operate the output buffer of PHP itself, so ob_flush only refresh PHP's own buffer, and flush is the buffer to flush Apache. Therefore, the correct use of the two order is: first Ob_flush, and then flush. Ob_flush is to release the data from the PHP buffer, flush is to send the buffer inside/out all the data to the browser.
3, do not mistakenly think that after using Ob_start (), the script Echo/print output will never be displayed on the browser. Because the PHP script finishes running, it automatically refreshes the buffer and outputs the content.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.