To implement gzip compression pages, the browser and the server need to support them. In fact, the server is compressed. After being uploaded to the browser, the browser decompress and parse the files. We don't need to worry about browsers, because most browsers now support parsing gzip pages. We only need to compress the page on the server and then output it to the browser.
A little wordy. Let's talk about the things below:
For example, to create a compressed biscuit, you must first obtain the raw material and compress a page. First, you must obtain the output content. The ob_start () (Ob => output buffer) function in PHP can implement this function. Program The content to be output is first put in a place called "buffer zone". Of course, you can think of it as a workbench for making compressed cookies and temporarily storing raw materials.
This function must be used before page output. Code . Because it is like a workbench, you have to prepare the raw materials before they arrive. Otherwise, there will be problems when the raw materials arrive. After we use ob_start () to get the page to be compressed, we can make the compressed biscuit. No, it should be possible to compress the page! However, it seems that there is still a lack of compressor, EZ. We use the zlib extension of PHP to create a compressor:
Copy code The Code is as follows: function ob_gzip ($ content) // $ content is the page content to be compressed, or the biscuit raw material.
{
If (! Headers_sent () & // If the page header information has not been output
Extension_loaded ("zlib") & // and zlib extensions have 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 the "// this page has been compressed" annotation label for the content to be compressed, and then use the gzencode () provided by zlib () the function execution level is 9. The value range of this parameter is 0-9. 0 indicates no compression, and 9 indicates the maximum compression. Of course, the higher the compression level, the higher the CPU cost.
// Then use the header () function to send some header information to the browser, telling the browser that the page has been compressed with gzip!
Header ("content-encoding: gzip ");
Header ("vary: Accept-encoding ");
Header ("Content-Length:". strlen ($ content ));
}
Return $ content; // return the compressed content, or send the compressed biscuit back to the workbench.
}
after the compressor is ready, we place the compressor on the workbench, so the original ob_start () is changed to
ob_start ('ob _ gzip '); // yes, add a parameter to ob_start (). The parameter name is the function name of the "compressor" We just created. In this way, when the content enters the buffer, PHP will call the ob_gzip function to compress it.
Well, all work has been completed, and the final delivery is:
ob_end_flush (); // end the buffer and output the content. Of course, this function is fine, because the buffer content is automatically output after the program is executed.
complete example: copy Code the code is as follows: // enable a workbench with an ob_gzip compressor
ob_start ('ob _ gzip ');
// prepare the content to be compressed
for ($ I = 0; $ I <100; $ I)
{< br> echo ('here is the raw material for compressing cookies, here is the raw material for compressing cookies ');
}< br> // output compression result
ob_end_flush ();
// This is the ob_gzip compressor
function ob_gzip ($ content)
{< br> If (! Headers_sent () &
extension_loaded ("zlib") &
strstr ($ _ server ["http_accept_encoding"], "gzip "))
{< br> $ content = gzencode ($ content. "\ N // This page is compressed", 9);
header ("content-encoding: gzip");
header ("vary: accept-encoding ");
header (" Content-Length :". strlen ($ content);
}< br> return $ content;
}< BR >?>
after actual tests, if Gzip is not used in the above Code, it is 4.69kb = 4802.56b. After Gzip is enabled, it is reduced to 104B ...... I may not be good at mathematics. I want to compress it to the original percentage ..
In addition, the following is the log information obtained using flashget. We can see the header information added to our program: copy Code the code is as follows: 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-: 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
Example 1 (using PHP's built-in compression function): copy Code the code is as follows: If (extension_loaded ('zlib ') ob_start ('ob _ gzhandler');
header ("Content-Type: text/html ");
?>
untitled document
for ($ I = 0; $ I <10000; $ I ++) {
echo 'Hello world! ';
}< BR >?>
If (extension_loaded ('zlib ') ob_end_flush ();
?>
Example 2: copy Code the code is as follows:
untitled document
ob_end_flush ();
// compression function
function ob_gzip ($ content) {
If (! Headers_sent () & extension_loaded ("zlib") & strstr ($ _ server ["http_accept_encoding"], "gzip ")) {
$ content = gzencode ($ content, 9);
header ("content-encoding: gzip");
header ("vary: accept-encoding ");
header (" Content-Length :". strlen ($ content);
}< br> return $ content;
}< BR >?>