The output Control of the PHP cache mechanism

Source: Internet
Author: User
Tags browser cache

---------------------------------------------------------------------------------------------------------

php5.2 version of the configuration, the default output_buffering is off state, run the following three lines of code will appear a warning:Warning: Cannot modify header information-headers Already sent

Echo ' Hello1 '; Header (' Content-type:text/html;charset=utf-8 '); Echo ' Hello2 ';

Two ways to turn on OB caching =

1. Open output_buffering = 4096 in php.ini

# with this directive enabled, each PHP script is equivalent to the first call to the Ob_start () function, PHP5.5 default is on output_buffering = 4096

2. Ob_start () directly in the program;

# Turn on output buffering. When the output buffer is activated, the script will not output content (except for the HTTP header), and the output will be stored in the internal buffer instead.

# The contents of the internal buffer can be copied into a string variable using the Ob_get_contents () function. To output content stored in an internal buffer, you can use the Ob_end_flush () function. In addition, using the Ob_end_clean () function silently discards the contents of the buffer.

/**
* output_buffering = off case test
* @chenwei <www.chenwei.ws>
*/
Ob_start(); // turn on the OB cache Echo // Deposit OB Cache Header (' Content-type:text/html;charset=utf-8 '); // Deposit Program Cache

Ob_end_clean (); Empties the OB cache and closes the OB cache

Echo' Hello2 ';//Deposit OB Cache$str=ob_get_contents();//Returnob-Cached data (does not clear buffered content)file_put_contents(' Ob.txt ',$str);//save $str to file
Ob_clean (); Emptying the OB cache
Echo' Hello3 ';//Deposit OB CacheEcho' Hello4 ';//Deposit OB Cache

/* This script will generate a ob.txt file, stored in Hello1hello2, browser output HELLO1HELLO2HELLO3HELLO4 */
/* If the Ob_clean () comment is open, the generated ob.txt file will have no content, the browser output HELLO3HELLO4 */
/* If the Ob_end_clean () comment is open, then there is still no content in Ob.txt because the OB cache is turned off, and the browser output HELLO2HELLO3HELLO4 */

Ob_flush () and Ob_end_flush () Examples:

Ob_start (); Echo    ' ABC '; Deposit the OB cache Header(' Content-type:text/html;charset=utf-8 ');//Deposit program Cache echo ' Hello '    ;    Deposit OB Cache Ob_flush(); Output the contents of the OB cache to the program cache, empty the OB cache, and do not close the OB cache
Ob_end_flush () //outputs the contents of the OB cache to the program cache, empties the OB cache, closes the OB cache echo ' AA '; Deposit OB Cache echoob_get_contents();

/* Last Output ABCHELLOAAAA */
/* Comment Ob_flush, open ob_end_flush, last output ABCHELLOAA */

[email protected] Black eyed poet <www.chenwei.ws>----------------------------------

Note: In case of output_buffering = 4096 turned on
Ob_end_clean () Closes the OB cache only once (that is, Ob_start is turned on) and the system is not turned off
Ob_end_flush () in the same vein.

Operating principle/principle of OB cache =

1. OB cache is turned on, echo data is first put into OB cache

2. If it is header information, put it directly in the program cache

3. When the page executes to the end, the OB cached data is placed in the program cache and then returned to the browser

  

Finally there is a flush (); Forces a refresh of the PHP program cache to the browser cache.

# Features: Some versions of Microsoft Internet Explorer will only start displaying the page after 256 bytes have been accepted, so you must send some extra spaces to let these browsers display the page content.

Echo str_repeat (', 1024); // output multiple characters repeatedly (resolves the browser cache after 256 bytes and then outputs the case)  for ($i$i$i+ +)    {echo$i;     Flush ();        // forcing the flush program to cache to the browser cache    Sleep (1);    // hibernate 1 seconds, HTTP connection not disconnected, output $i} Every 1 seconds

---------------------------------------------------------------------------------------------------------

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.