Use PHP to control the use of the user's browser-ob * Function

Source: Internet
Author: User

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 by header () or setcookie (). It only applies to those similar to echo () and php (as the mainstream development language) the data block of the code works.

  

Let's take a simple example to give you a general impression on Output Control:
Example 1.

Ob_start (); // open the buffer
Echo "Hellon"; // output
Header ("location: index. php (as the current mainstream development language)"); // redirect the browser to index. php (as the current mainstream development language)
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!

1. Related functions
1. Flush: refresh the buffer content and output it.
Function Format: flush ()
Note: 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 zone is activated, all non-file header information from php (as the current mainstream development language) programs will not be sent, but will be stored in the internal buffer zone. To output the buffer content, you can use ob_end_flush () or flush () to output the buffer content.

3. ob_get_contents: returns the content of the internal buffer.
Usage: string ob_get_contents (void)
Note: This function returns the content in the Current Buffer. If the output buffer is not activated, FALSE is returned.

4. ob_get_length: return the length of the internal buffer.
Usage: int ob_get_length (void)
Note: This function returns the length of the Current Buffer. It is the same as ob_get_contents if the output buffer is not activated. Returns FALSE.

5. ob_end_flush: sends the content of the internal buffer to the browser and closes the output buffer.
Usage: void ob_end_flush (void)
Note: This function sends the content of the output buffer (if any ).

6. ob_end_clean: Delete the content of the internal buffer and disable the internal buffer.
Usage: void ob_end_clean (void)
Note: This function will not output the content of the internal buffer but delete it!

7. ob_implicit_flush: enable or disable absolute refresh
Usage: void ob_implicit_flush ([int flag])
Note: Anyone who has used Perl knows the meaning of $ | = x. This string can enable/disable the buffer, while the ob_implicit_flush function is the same as that. The buffer is disabled by default, after the absolute output is enabled, each Script output is directly sent to the browser, and you do not need to call flush ()

2. In-depth understanding

1. About the Flush function:
This function appears in php (as the mainstream development language) 3. It is a very efficient function. A very useful function is to refresh the browser cache. we will give an example with very obvious running effect to illustrate flush.
Example 2.

For ($ I = 1; $ I <= 300; $ I ++) print ("");
// This sentence is critical. The cache structure makes it output from the browser only when the content reaches a certain size.
// In other words, if the cache content does not reach a certain size, it will not be output before the program is executed. Jing
// After testing, I found that the base size is 256 characters long. This means that all content received by the cache will be
// Continuously sent out.
For ($ j = 1; $ j <= 20; $ j ++ ){
Echo $ j ."
";
Flush (); // This part will squeeze out the new content of the cache and display it on the browser.
Sleep (1); // Let the program "sleep" for one second, so that you can see the effect more clearly
}

?>

Specific effects you can look at here http://www.php (as the mainstream development language) 2000.com /~ Uchinaboy/out. php (as the mainstream development language)
Php (as the current mainstream development language) 2000 of the latest php (as the current mainstream development language) chat room is using this technology, unfortunately, the source code is not open L
Note: If you add ob_implicit_flush () to the program header to enable absolute refresh, you can stop using flush () in the program. The advantage of doing this is to improve efficiency!

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.