Detailed explanation of PHP cache mechanism Output Control

  • 2021-07-09 07:31:23
  • OfStack

In the configuration of php version 5.2, output_buffering is turned off by default, so running the following three lines of code will cause 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 ways to turn on OB cache:

1. Enable output_buffering = 4096 in php. ini

When this directive is enabled, each PHP script calls the ob_start () function from the start of 1, and PHP5.5 has output_buffering = 4096 on by default

2. Use ob_start () directly in the program;

Open the output buffer. When the output buffer is activated, the script will not output the content (except the http header). Instead, the content that needs to be output is stored in an internal buffer.

The contents of the internal buffer can be copied into a string variable using the ob_get_contents () function. To output what is 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  Condition test 
 */
ob_start();  // Open ob Cache 
echo 'hello1'; // Deposit ob Cache 
header('content-type:text/html;charset=utf-8');// Store in program cache 
//ob_end_clean(); // Empty ob Cache and close ob Cache 
echo 'hello2'; // Deposit ob Cache 
$str = ob_get_contents(); // Return ob Cached data (cached contents are not cleared) 
file_put_contents('ob.txt', $str); // Put $str Save to File 
//ob_clean(); // Empty ob Cache 
echo 'hello3'; // Deposit ob Cache 
echo 'hello4'; // Deposit ob Cache 
/*  This script generates the ob.txt File, save in hello1hello2 Browser output hello1hello2hello3hello4 */
/*  If ob_clean() Annotation opens, the generated ob.txt There will be no content in the file, and the browser will output it hello3hello4 */
/*  If ob_end_clean() Comment opens, then ob.txt There is still no content in the ob Cache, browser output hello2hello3hello4 */

Examples of ob_flush () and ob_end_flush ():


ob_start();
echo 'abc';// Deposit ob Cache 
header('content-type:text/html;charset=utf-8'); // Store in program cache 
echo 'hello'; // Deposit ob Cache 
ob_flush();// Will ob Output the contents in the cache to the program cache and empty it ob Cache, do not close ob Cache 
//ob_end_flush() // Will ob Output the contents in the cache to the program cache and empty it ob Cache, close ob Cache 
echo 'aa'; // Deposit ob Cache 
echo ob_get_contents();
/*  Final output abchelloaaaa */
/*  Notes ob_flush, Open ob_end_flush, Final output abchelloaa */

Note:
When output_buffering = 4096 is turned on, ob_end_clean () only closes ob cache once (i.e. ob_start is turned on), and the system is not turned off.
The same applies to ob_end_flush ().

How OB cache works/principles:

1. The ob cache is opened, and the data of echo is first put into the ob cache
2. If it is header information, put it directly in the program cache
3. When the page is executed to the end, the data cached by ob will be put into the program cache, and then returned to the browser once

Finally, there is an flush (); Forcibly flush the PHP program cache to the browser cache.

Features: Some versions of Microsoft Internet Explorer do not display the page until 256 bytes have been received, so one extra space must be sent for these browsers to display the page content.


echo str_repeat('', 1024);// Output multiple characters repeatedly (solve browser cache) 256 Bytes before output) 
for($i=0; $i < 5; $i++)
{
  echo $i;
  flush();    // Force a refresh of the program cache to the browser cache 
  sleep(1);      // Dormancy 1 Seconds, http The connection is not disconnected, and every time 1 Second output $i
}

Related articles: