PHP cache mechanism output control, outputcontrol_php tutorial

Source: Internet
Author: User

PHP cache mechanism Output control detailed, OutputControl


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

There are two 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 calling the Ob_start () function from the beginning, and PHP5.5 is turned on by default output_buffering = 4096

2. use 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 */ob_start ();  Turn on OB cache echo ' Hello1 '; Deposit the OB cache header (' content-type:text/html;charset=utf-8 ');//Deposit Program Cache//ob_end_clean (); Empty the OB cache and turn off the OB cache echo ' Hello2 '; Deposit OB Cache $str = Ob_get_contents (); Returns the data for the OB cache (does not clear the buffered content) file_put_contents (' Ob.txt ', $str); Save $str to File//ob_clean (); Empty OB cache echo ' Hello3 '; Deposit OB Cache echo ' Hello4 '; Deposit OB Cache/* This script will generate Ob.txt file, deposit Hello1hello2, browser output Hello1hello2hello3hello4 *//* if Ob_clean () Comment open, Then there will be no content in the generated ob.txt file, 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, the browser output Hello2hello3hello4 */

Ob_flush () and Ob_end_flush () Examples:

Ob_start (); Echo ' abc ';//Deposit OB Cache header (' Content-type:text/html;charset=utf-8 '); Deposit program Cache echo ' Hello '; Deposit OB Cache Ob_flush ();//outputs the contents of the OB cache to the program cache, empties the OB cache, does 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 echo ob_get_contents ();/* Last output abchelloaaaa *//* comment ob_flush, open ob_end_flush, last output ABCHELLOAA */

Attention:
In the case of output_buffering = 4096, Ob_end_clean () only shuts down the OB cache (that is, Ob_start 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.

Feature: Some versions of Microsoft Internet Explorer only start to display 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);//Repeat output of multiple characters (to resolve the browser cache after 256 bytes to output the case) for ($i =0; $i < 5; $i + +) {  echo $i;  Flush ();    Force Flush Program Cache to browser cache  sleep (1);  Hibernate 1 seconds, HTTP connection not disconnected, output $i} every 1 seconds

How to use the cache in PHP, which is the best way to use the caching mechanism;

This depends on your actual situation, there are file cache, database cache, and Memcache cache .....

What is the caching mechanism for PHP?

Mainly include:
① Universal Caching Technology ② page cache ③ time trigger cache ④ content triggers cache ⑤ static cache (generates HTML files)
⑥ Memory Cache ⑦php Buffer ⑧mysql Cache ⑨ Reverse proxy based Web cache, DNS polling
But generally commonly used on the ①②④, the other is the Web site data volume, interaction, in order to reduce the server pressure to use
Reference: blog.163.com/...44905/

http://www.bkjia.com/PHPjc/840629.html www.bkjia.com true http://www.bkjia.com/PHPjc/840629.html techarticle PHP cache mechanism output control, OutputControl in the php5.2 version of the configuration, the default output_buffering is off, so run the following three lines of code will appear a warning: ...

  • 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.