Use PHP to control your browser cache | use PHP to control your browser

Source: Internet
Author: User
The OutputControl function allows you to 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 apply header () or setcookie (). The Output Control function allows you to Control the data Output 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 the application header () or setcookie (). it only applies to data blocks similar to echo () and PHP code.

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

Example 1.

<? Php
Ob_start (); // open the buffer
Echo 'hellon'; // output
Header ("location: index. php"); // redirects the browser to index. php.
Ob_end_flush (); // output all content to the browser
?>

Anyone who knows the header () function knows that this function will send a file header to the browser, but if there is any output (including null output, for example, a blank space, carriage return, or line feed will prompt an error. If we drop the ob_start () in 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 is opened, the characters after echo will not be output to the browser, but will be retained on the server until you apply flush or ob_end_flush, so there will be no file header output errors!

   1. Introduction to coherent functions:

1. Flush: refresh the buffer content and output it.

Function pattern: flush ()
Clarification: This function is often used and highly efficient.

2. ob_start: Open the output buffer.

Function pattern: void ob_start (void)
Note: When the buffer zone is activated, all non-file header information from the PHP program is not sent, but is kept in the internal buffer zone. To output the buffer content, you can apply ob_end_flush () or flush () to output the buffer content.

3. ob_get_contents: returns the content of the internal buffer.

Application method: string ob_get_contents (void)
Clarification: 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.

Application method: int ob_get_length (void)
Clarify: This function will return the length of the current buffer; 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.

Application method: void ob_end_flush (void)
Clarify: This function sends the content of the output buffer (if any ).

6. ob_end_clean: delete the content of the internal buffer and close the internal buffer.

Application method: void ob_end_clean (void)
Clarify: This function will not output the content of the internal buffer but delete it!

7. ob_implicit_flush

Application method: void ob_implicit_flush ([int flag])
Clarification: anyone who has applied Perl knows the meaning of $ | = x. This string can open/close the buffer, and the ob_implicit_flush function is the same as that. the default value is closed buffer, after the correct output is enabled, each script output is directly sent to the browser, and you do not need to call flush ()

   2. Deep understanding:

1. about the Flush function:

This function is displayed in PHP3. it is a very efficient function. it has a very useful function that is to refresh the browser cache. let's give an example with obvious running consequences to illustrate flush.

Example 2.

<? Php
For ($ I = 1; $ I <= 300; $ I) print ("");
// This sentence is very important. the cache structure makes its content only reach a certain size and can be output from the browser.
// In other words, if the cache content does not reach a certain size, it will not be output before the program is completed. Jing
// After testing, I found that the size limit 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); // letting the program "sleep" for one second will make you better understand the consequences
}
?>

Note: If you participate in ob_implicit_flush () in the program's first step, you can stop applying flush () in the program. The benefit of doing so is to improve efficiency!

2. ob functions:

I would like to first reference an example of my good friend y10k:

Example 3.

For example, if you want to retain the output of the phpinfo () function, you can obtain the configuration information of the server and the client? Before the buffer control, we can say that there are no measures, but with the buffer control, we can easily solve the problem:

<? Php
Ob_start (); // open the buffer
Phpinfo (); // apply the phpinfo function
$ Info = ob_get_contents (); // Obtain the buffer content and assign it to $ info
Using file1_fopen('info.txt ', 'w'); // open the info.txt file.
Fwrite ($ file, $ info); // write the information to info.txt
Fclose ($ file); // closed file info.txt
?>

With the above method, we can retain the phpinfo information of different users. I am afraid there is no way to do this before! The above is how to convert some "procedures" into "functions!

Someone may ask, "is it like this? Is there any other use ?" Of course, for example, the PHP syntax highlighted by the author's forum is related to this. (the default PHP syntax highlighted by the display function will be directly output and the results cannot be retained, I am afraid it will be a waste of CPU resources if it is displayed in every call!

Maybe now everyone has a certain understanding of the effectiveness of ob_start (), the above example seems simple, but actually has grasped the key points of the application of ob_start.

<1> The application ob_start opens the browser cache. This ensures that the cache content will not be output before you call flush (), ob_end_flush () (or after the program is executed.

<2>. now you should know the advantages you have: you can apply the header, setcookie, and session after any output content, which is a great feature of ob_start. you can also apply the ob_start parameter, after the cache is written into, you can actively run commands, such as ob_start ('OB _ gzhandler'). The most common practice is to use ob_get_contents () to get the content in the cache, and then proceed ......

<3>. after processing is completed, we can apply various methods for output, flush (), ob_end_flush (), and the active output after the program is executed. Of course, if you use ob_get_contents (), then the worker will control the output method.

Let's see what we can do with ob series functions ...... [1] [2] Next page 

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.