PHP caching mechanism output control detailed _php tips

Source: Internet
Author: User
Tags comments php script browser cache

In the php5.2 version of the configuration, the default output_buffering is off, so running the following three lines of code will show a warning:
Warning:cannot Modify header Information-headers already sent

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

There are two different ways to turn on the OB cache:

1. Open output_buffering = 4096 in php.ini

With this directive enabled, each PHP script is equivalent to invoking the Ob_start () function at the beginning, PHP5.5 the default is open output_buffering = 4096

2. use of Ob_start () directly in the procedure;

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

The contents of the internal buffer can be copied into a string variable using the Ob_get_contents () function. To output the content stored in the 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 Test
/Ob_start ();  Turn on OB cache
echo ' hello1 ';///Save to OB cache
header (' content-type:text/html;charset=utf-8 ');//Save to program cache
//ob_end_ Clean (); Empty the OB cache and turn off the OB cache
echo ' Hello2 ';//Save OB cache
$str = ob_get_contents ();//return OB cached data (do not clear buffered content)
File_put_ Contents (' Ob.txt ', $str); Save $str to File
//ob_clean ();//Empty OB cache
echo ' Hello3 ';////Storage in OB cache
echo ' Hello4 ';//in OB cache/
* This script will generate Ob.txt files, save in Hello1hello2, browser output HELLO1HELLO2HELLO3HELLO4
/* If Ob_clean () comments Open, Then the generated ob.txt file will not have content, the browser output HELLO3HELLO4 * *
if Ob_end_clean () comments Open, So 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 ';//Save OB Cache
Header (' content-type:text/html;charset=utf-8 ');//Save to program cache
echo ' Hello ';
_flush ()///outputs the contents of the OB cache to the program cache, empties the OB cache, does not turn off the OB cache
//ob_end_flush ()///outputs the contents of the OB cache to the program cache, empties the OB cache, and turns off the OB cache
echo ' AA '; /stored in OB cache
echo ob_get_contents ();
/* The final output abchelloaaaa * * *
note ob_flush, open Ob_end_flush, the final output ABCHELLOAA * *

Attention:
when output_buffering = 4096 is turned on, the Ob_end_clean () closes only one OB cache (that is, Ob_start), and the system is not closed.
Ob_end_flush () in the same vein.

Running principle/Principle of OB caching:

1. OB cache is turned on, echo data is first put into OB cache
2. If header information is placed directly in the program cache
3. When the page executes to the end, the OB-cached data is put into the program cache and then returned to the browser once

Finally there is a flush (); Force the 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 received, so you must send some extra spaces to allow the browsers to display the page content.

 Echo str_repeat (', 1024);//multiple characters repeatedly output (after the browser caches 256 bytes and then output) for ($i =0; $i < 5; $i + +) {
  echo $i;    Flush ();    Force Flush Program Cache to browser cache sleep (1); Hibernate 1 seconds, HTTP connection is 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.