Control the output of PHP: cache and compress dynamic pages

  • 2020-06-12 08:42:56
  • OfStack

mod_gzip is an Apache module that USES Gzip to compress static html pages. IETF compliant browsers can accept gzip encoding (IE, Netscape, etc.). mod_gzip can increase the download time of pages by 4-5 times. I strongly recommend that you use mod_gzip on your web server. However, we also had to build our own compression engine with PHP. In this article, I'll show you how to use PHP's output control functions to significantly speed up page load times.

Introduces the output control function of PHP

The best thing about PHP4 is that you can have PHP cache all the output generated by the script, and the browser won't receive anything until you decide to send it out. You can use these functions to set header and cookies in a script, however this is only a small part of the powerful output functions.


<?php 
void ob_start(void); 
?> 

Tell the PHP processor to redirect all output to an internal cache (buffer). No output is sent to the browser before calling ob_start.


<?php 
string ob_get_contents(void); 
?> 

This function returns the output cache (output buffer) as a string. You can call this function to send the accumulated output to the browser. (Only after buffering is turned off!!)


<?php 
int ob_get_length(void); 
?> 

Returns the length of the string in the cache.


<?php 
void ob_end_clean(void); 
?> 

Clear the output cache and turn off the output cache. This function must be used before the contents of the cache are output to the browser.
void 501([int flag])
Used to turn on/off the implied flush action switch (off by default). If flush is on, every time you call print/echo or some other output command, the output is immediately sent to the browser.

Use output control to compress the PHP output
You must use the Zlib extension compiled in PHP4 to compress the output. If needed, check out the Zlib package installation instructions in the PHP documentation.
First, initialize the output cache:


<?php 
ob_start(); 
ob_implicit_flush(0); 
?> 

After that, generate all the output using print, echo, or whatever method you like, for example:


<?php
print("Hey this is a compressed output!"); 
?> 

After the page is generated, we get back the output:


<?php 
$contents = ob_get_contents(); 
ob_end_clean(); 
?> 

After that, you must detect whether the browser supports compression of the data. If it does, the browser sends an ES86en-ES87en HTTP header to the server. We just need to check for the "gzip,deflate" string in the $HTTP_ACCEPT_ENCODING variable.


<?php 
if(ereg('gzip, deflate',$HTTP_ACCEPT_ENCODING)) { 
//  I'm going to generate here  Gzip  Compressed content  
} else { 
echo $contents; 
} 
?> 

This method is simple to use and clear in structure. Let's look at how to generate compressed output:


<?php 
// Tell the browser what to expect gzip data  
// Before that, of course, you've checked to see if they're supported gzip . x-gzip The data format  
// If the support is x-gzip And then the bottom head is used z-gzip To take the place of  
header("Content-Encoding: gzip"); 
// According to gzip The head of the file  
// Just show 1 time  
echo "x1fx8bx08x00x00x00x00x00"; 
// Calculate the size and size of the file CRC code  
$Size = strlen($contents); 
$Crc = crc32($contents); 
// Compressed data  
$contents = gzcompress($contents, 9); 
// We can't just export it because CRC The code is messy.  
// If I use" echo $contents " , The compressed data will be sent out,  
// But it's incomplete. Last of the file 4 A byte is CRC Check code, but only sent out 3 Bytes.  
// The last 1 Two bytes are thrown away. I didn't know that bug in 4.0.2 Has it been resolved in the version,  
// But the best way to avoid making mistakes is to make the right ones CRC The check code is added to the end of the compressed data.  
// 
// The old CRC Check code stripping  
$contents = substr($contents, 0, strlen($contents) - 4); 
// Only compressed data is displayed  
echo $contents; 
// The output CRC , and the size (bytes) of the original data  
gzip_PrintFourChars($Crc); 
gzip_PrintFourChars($Size); 
function gzip_PrintFourChars($Val) { 
for ($i = 0; $i <4; $i ++) { 
echo chr($Val % 256); 
$Val = floor($Val / 256); 
} 
} 
?> 
// Well, you can also attach more compressed data in this way.  

For the actual test, all the script code is as follows:


<?php 
ob_start(); 
ob_implicit_flush(0); 
print("I'm compressed!n"); 
$contents = ob_get_contents(); 
ob_end_clean(); 
header("Content-Encoding: gzip"); 
echo "x1fx8bx08x00x00x00x00x00"; 
$Size = strlen($contents); 
$Crc = crc32($contents); 
$contents = gzcompress($contents, 9); 
$contents = substr($contents, 0, strlen($contents) - 4); 
echo $contents; 
gzip_PrintFourChars($Crc); 
gzip_PrintFourChars($Size); 
function gzip_PrintFourChars($Val) { 
for ($i = 0; $i <4; $i ++) { 
echo chr($Val % 256); 
$Val = floor($Val / 256); 
} 
} 
?> 

Cache PHP output

When PHP4 didn't exist and I had to use PHP3, I was interested in developing a caching mechanism to reduce database load and file system access by 10 points. There is nothing particularly good about PHP3, but with the output cache, 1 shear is much easier in PHP4.
Here's a simple example:


<?php 
string ob_get_contents(void); 
?> 
0

This is a simple example. Using output caching, you can build a complex content generation system, use different caching mechanisms for different blocks or programs, and so on...

conclusion

The PHP output control function is useful for redirecting script generated output to the cache for 10 points. Loading time can be reduced by outputting cached data for gzip-enabled browsers. It can also be used as a caching mechanism to reduce access to data sources (databases or files), which is significant when using XML.
If we build an engine with PHP, cache the data from the data source (xml documents and databases), and dynamically generate XML content (no external-ES133en) we can take the output of these XML and use XSLT to convert to any desired external-format (html, wap, palm, pdf, etc.). The PHP4 output cache and the Sablotron XSLT extension do the job nicely.


Related articles: