PHP zlib extension to achieve page GZIP compression output

  • 2020-03-31 20:53:55
  • OfStack

To implement GZIP compression of a page requires both the browser and the server to support it, which is actually server compression, which is unzipped and parsed by the browser after it is sent to the browser. We don't need to worry on the browser side because most browsers now support parsing GZIP pages. We simply compress the page on the server and output it to the browser.

A bit wordy, but here's the deal:

Just like making a compressed cookie, you have to get the ingredients, you have to compress a page, you have to get the output. Ob_start () (ob => The output buffer function does this by placing the output of the program in a place called a buffer.
This function must be used before the output of the page, so it is usually placed at the top of the code. Because it's like a workbench, you have to have it ready before the raw material comes in, otherwise the raw material comes in and there's nowhere to put it, and it's going to be a problem. With ob_start() to get the page to be compressed, we're ready to make a compressing cookie. However, it seems that there is still a compressor missing. EZ, we use PHP with the zlib extension to make one:
 
function ob_gzip($content) //$content is the content of the page to be compressed, or the raw cookie
{ 
if( !headers_sent() && //If the header information is not yet output
extension_loaded("zlib") && //And the zlib extension has been loaded into PHP
strstr($_SERVER["HTTP_ACCEPT_ENCODING"],"gzip")) //And the browser says it can accept GZIP pages
{ 
$content = gzencode($content." n// This page has been compressed ",9);  Paste" // This page has been compressed "comment label, then use zlib To provide the gzencode() The execution level of the function is 9 The value range of this parameter is 0-9 . 0 Means no compression, 9 Represents the maximum compression, of course, the higher the compression degree, the more expensive CPU .  

//Then use the header() function to send some header information to the browser that the page has been GZIP compressed!
header("Content-Encoding: gzip"); 
header("Vary: Accept-Encoding"); 
header("Content-Length: ".strlen($content)); 
} 
return $content; //Returns the compressed contents, or returns the compressed cookies to the workbench.
} 

Once the compressor is done, we put the compressor on the workbench, so the ob_start() becomes the ob_start()

Ob_start (' ob_gzip '); // yes, that is to add a parameter to ob_start(), the parameter name is the function name of the "compressor" we just made. This way, when the content enters the buffer, PHP will call the ob_gzip function to compress it.
Well, all the work is done, and the final delivery:

Ob_end_flush (); // end buffer, output contents. Of course, you can do without this function, because the program will automatically print out the buffer contents at the end of execution.
The complete example is as follows:
 
<?php 
//Enable a workbench with an ob_gzip compressor
ob_start('ob_gzip'); 
//Prepare something to compress
for($i=0; $i<100; $i++) 
{ 
echo(' This is the raw material for the compressing cookie, this is the raw material for the compressing cookie, the raw material '); 
} 
//Output compression
ob_end_flush(); 
//This is the ob_gzip compressor
function ob_gzip($content) 
{ 
if( !headers_sent() && 
extension_loaded("zlib") && 
strstr($_SERVER["HTTP_ACCEPT_ENCODING"],"gzip")) 
{ 
$content = gzencode($content." n//This page has been compressed ",9);

header("Content-Encoding: gzip"); 
header("Vary: Accept-Encoding"); 
header("Content-Length: ".strlen($content)); 
} 
return $content; 
} 
?> 

After actual testing, if you don't use GZIP in the above code, it's 4.69kb = 4802.56b. After enabling GZIP, it's shrunk to 104B, er... I may not be good at math, their own reduction to the original how many percent.

In addition, the following is the log information obtained with FlashGet, you can see the header information in our program:
 
Fri Jan 25 17:53:10 2008 HTTP/1.1 200 OK 
Fri Jan 25 17:53:10 2008 Server: Microsoft-IIS/5.1 
Fri Jan 25 17:53:10 2008 Date: Fri, 25 Jan 2008 09:53:10 GMT 
Fri Jan 25 17:53:10 2008 Connection: close 
Fri Jan 25 17:53:10 2008 X-Powered-By: PHP/5.2.5 
Fri Jan 25 17:53:10 2008 Content-Encoding: gzip 
Fri Jan 25 17:53:10 2008 Vary: Accept-Encoding 
Fri Jan 25 17:53:10 2008 Content-Length: 104 
Fri Jan 25 17:53:10 2008 Content-type: text/html 

Related articles: