Use PHP's Ob_start () to control your browser cache_php tips

Source: Internet
Author: User
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.
Let's start with a simple example that gives you a general impression of output control:
Example 1.
Copy Code code as follows:

<?php
Ob_start (); Open buffer
echo \ "Hellon\"; Output
Header ("location:index.php"); redirect 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 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!
Introduction of related functions:
1, Flush: Refresh the contents of the buffer, output.
function format: Flush ()
Description: This function is frequently used and highly efficient.
2, Ob_start: Open the output buffer
function format: void Ob_start (void)
Note: When the buffer is activated, all non-file header information from the PHP program is not sent, but is saved in the internal buffer. To output the contents of the buffer, you can use Ob_end_flush () or flush () to output the contents of the buffer.
3, Ob_get_contents: Return the contents of the internal buffer.
Using method: String ob_get_contents (void)
Description: This function returns the contents of the current buffer and FALSE if the output buffer is not active.
4, Ob_get_length: Returns the length of the internal buffer.
How to use: int ob_get_length (void)
Description: This function returns the length of the current buffer, as ob_get_contents if the output buffer is not active. Returns FALSE.
5, Ob_end_flush: Send the contents of the internal buffer to the browser, and turn off the output buffer.
Usage method: void Ob_end_flush (void)
Description: This function sends the contents of the output buffer (if any).
6, Ob_end_clean: Delete the contents of the internal buffer, and close the internal buffer
Usage method: void Ob_end_clean (void)
Description: This function does not output the contents of the internal buffer but deletes it!
7. Ob_implicit_flush: Turn absolute refresh on or off
How to use: void Ob_implicit_flush ([int flag])
Description: People who have used Perl know the meaning of $|=x, this string can open/close the buffer, and the Ob_implicit_flush function is the same as that, the default is to close the buffer, open absolute output, each script output is sent directly to the browser, no longer need to call the flush ()
Second, in-depth understanding:
1. About the Flush function:
This function appears in the PHP3, is a very efficient function, he has a very useful function is to refresh the browser cache. We give a very obvious example of the effect of the operation to illustrate the flush.
Example 2.
Copy Code code as follows:

<?php
for ($i = 1; $i <=; $i + +) print ("");
//This sentence is critical, the cache structure so that its content can only reach a certain size in order to output from the browser
//In other words, if the cache content does not reach a certain size, it will not be output before the execution of the program. After the
//over test, I found that the limit of this size is 256 characters in length. This means that the content received after the cache will be
//continuously sent out.
for ($j = 1; $j <= $j + +) {
echo $j. "
";
Flush ();//This will cause the cache additions to be squeezed out and displayed to the browser
Sleep (1);//Let the program "Sleeps" for a second, which will make you see the effect more clearly
}
?>

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.