PHP output Cache OB series functions detailed _php Tips

Source: Internet
Author: User
Tags file info flush php script phpinfo setcookie

The basic principle of OB: If the OB cache is turned on, the Echo data is first placed in the OB cache. If header information is placed directly in the program cache. When the page executes to the end, the OB-cached data is put into the program cache and then returned to the browser in turn.
Let me talk about OB's basic role:
1 prevents errors caused by functions such as Setcookie (), header (), or session_start () to send header files after the browser has output. In fact, this use less for good, develop good code habits.
2 capturing the output of some unreachable functions, such as phpinfo (), will output a large stack of HTML, but we cannot use a variable such as $info=phpinfo () to capture, at which point OB works.
3 Processing of output content, such as gzip compression, for example, for simple conversion, such as some string substitution.
4 Generate a static file, in fact, is to capture the entire page output, and then save as a file. Often used in generating HTML, or in full-page caching.

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

Copy Code code as follows:
Ob_start (Ob_gzhandler);
The content to be cached

Yes, add a Ob_gzhandler this callback function, but there are some minor problems, one is the need for zlib support, the second is not to determine whether the browser support gzip (now seems to support, the iphone browser seems to support).
The previous approach was to determine if the browser supports gzip and then compress the contents of ob_get_contents () with a third party gzip function, and finally echo.

A collection of functions commonly used in OB series functions

Copy Code code as follows:

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

The

Ob_clean ();           //Deletes the contents of the internal buffer without closing the buffer (not output). The
Ob_end_clean ();       //Deletes the contents of the internal buffer and closes the buffer (not output). The
Ob_get_clean ();       //Returns the contents of the internal buffer and closes the buffer. Equivalent to executing ob_get_contents () and Ob_end_clean ()
Ob_flush ();            //Send the contents of the internal buffer to the browser, delete the contents of the buffer, and do not close the buffer. The
Ob_end_flush ();       //sends the contents of the internal buffer to the browser, deletes the contents of the buffer, and closes the buffer. The
Ob_get_flush ();       //Returns the contents of the internal buffer, closes the buffer, and then releases the contents of the buffer. Corresponds to Ob_end_flush () and returns the contents of the buffer.
Flush ();              //Will Ob_ Flush released content, and not in the PHP buffer content, all output to the browser, refresh the contents of the internal buffer, and output.

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 to 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 to OFF, Ob_implicit_flush (true) after open, and absolute refresh, that is, when an output statement (E.g:echo) is executed, the output is sent directly to the browser without the need to invoke flush () Or wait until the end of the script to output.

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

Second, the example explanation

1, the header () function can have the Echo code before
The output control function gives you a free rein in the outputs of the data in the script. It's very useful, especially for: when you want to output the file header after the data has been exported.
Output control functions do not use header () or Setcookie (), send header information to affect, only for those similar to echo () and PHP code blocks of data have effect.

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

All people who know about the header () function know that this function sends a file header to the browser, but if you have any output (including null output, such as a space, a carriage return, or a newline) before using this function, you will be prompted for an error. If we remove the first line of Ob_start () and then execute the program, we will find an error message: "Header had all ready send by"! But with Ob_start, you won't be prompted for an error, because when the buffer is open, the characters behind Echo are not exported to the browser, but remain on the server until you use flush or ob_end_flush to output, so there is no error in the header output!
2, save the output of the Phpinfo () function
Copy Code code as follows:
Ob_start (); Open buffer
Phpinfo (); Using the Phpinfo function
$info = Ob_get_contents (); Gets the contents of the buffer area and assigns it 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 the HTML page is not updated, then when another user browses 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 this kind of technology are enormous.
Copy Code code as follows:
Ob_start (); Open buffer
All output of PHP page
$content = Ob_get_contents (); Get all the content of the PHP page output
$fp = fopen ("output00001.html", "w"); Create a file and open it, ready to write
Fwrite ($fp, $content); Write the contents of the PHP page to output00001.html, and 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 different usage. The contents to be added to the php.ini when using Ob_gzhandler are as follows:
Copy Code code as follows:
Output_handler = Ob_gzhandler;

This line of code enables PHP to activate the output cache and compress all the content it sends out.

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

Copy Code code as follows:
Php_value Output_handler Ob_gzhandler

or call from the PHP code, as follows:

Copy Code code as follows:
Ob_start ("Ob_gzhandler");

The method of using output-cache handles is really very effective and does not give the server any special load. However, it is important to note that Netscape Communicator has poor support for compressed graphics, so you should suppress JPEG and GIF graphics unless you can guarantee that all users will use IE. Generally, this compression works for all other files, but it is recommended that you test the various browsers individually, especially if you are using a special plugin or a data viewer.
Precautions :
1, some Web server output_buffering default is 4069 characters or greater, that is, the output must reach 4069 characters server will flush refresh output buffer, in order to ensure flush effective, preferably in the Ob_flush () function before the following statements:
Copy Code code as follows:
Print Str_repeat ("", 4096); To ensure that the output_buffering value is reached

2. The Ob_* series function is the output buffer that operates PHP itself, so ob_flush only refreshes the buffer of PHP itself, while flush is refreshing the Apache buffer. Therefore, the correct use of the two order is: first Ob_flush, and then flush. Ob_flush is the release of data from the PHP buffer, flush is to send the buffer inside/out of the data to the browser.
3, do not mistakenly believe that the use of Ob_start (), the script's echo/print output will never be displayed in the browser. Because the PHP script runs, it automatically refreshes the buffer and outputs the contents.

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.