To achieve the GZIP compression page requires a common browser and server support, is actually the server compression, uploaded to the browser after the browser decompression and resolution. Browsers don't need us to worry because most browsers now support parsing gzip-enabled pages. All we need to do is compress the page on the server side and then output it to the browser.
A little wordy, here's the thing:
Just as to make a compressed biscuit, first to get the raw material, to compress a page, first to get the content to output. The Ob_start () (OB => output buffer) function in PHP allows you to put the contents of the program's output into a place called a "buffer", which, of course, can be understood as a platform for making compressed cookies for temporary release.
This function must be used before the page output, so it is generally placed at the top of the code. Because it is like a workbench, so you have to be prepared before the arrival of raw materials, or raw materials come to no place to put, there will be problems. With Ob_start () to compress the page, we can make compression cookies, no, it should be able to compress the page! But there seems to be a lack of a compressor, EZ, we use PHP with the zlib expansion to do one:
Copy Code code as follows:
function Ob_gzip ($content)//$content is the content of the page to be compressed, or the cookie material.
{
if (!headers_sent () &&//If the page header information has not yet been exported
extension_loaded ("zlib") &&/and zlib extensions have been loaded into PHP
Strstr ($_server["http_accept_encoding"], "gzip")//and browser says it can accept gzip pages
{
$content = Gzencode ($content. "\n//This page has been compressed", 9); Post a "//this page compressed" comment label for the content being compressed, and then perform a 9-level compression with the Gzencode () function provided by zlib, which ranges from 0-9,0 to zero compression, and 9 for maximum compression, which, of course, costs more CPU.
Then use the header () function to send some header information to the browser, tell the browser this 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 cookie back to the workbench.
}
After the compressor is done, we put the compressor on the workbench, so the original Ob_start () becomes
Ob_start (' Ob_gzip '); Yes, is to give Ob_start () add a parameter, the parameter name is we just do the "compressor" function name. So when the content goes into the buffer, PHP calls the Ob_gzip function to compress it.
Well, all the work has been done, the final delivery:
Ob_end_flush (); End buffer, output content. Of course, this function is also OK, because the program will automatically output the buffer content at the end of execution.
The complete example is as follows:
Copy Code code as follows:
<?php
Enable a workbench with a ob_gzip compressor
Ob_start (' Ob_gzip ');
Prepare some content to be compressed
for ($i =0; $i <100; $i)
{
Echo (' Here is the raw material for compressing cookies, here is the raw material for compressing cookies ');
}
Output compression Results
Ob_end_flush ();
This is 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 the actual test, the above code if not gzip, is 4.69kb=4802.56b, enabling gzip reduced to 104B, er ... I may not be good at math, I have compressed to the original percentage of it.
In addition, the following is the log information obtained with FlashGet, you can see the header information in our program Riga:
Copy Code code as follows:
Fri 17:53:10 2008 http/1.1 OK
Fri 17:53:10 2008 server:microsoft-iis/5.1
Fri 17:53:10 2008 Date:fri, 2008 09:53:10 GMT
Fri 17:53:10 2008 Connection:close
Fri 17:53:10 2008 x-powered-by:php/5.2.5
Fri 17:53:10 2008 Content-encoding:gzip
Fri 17:53:10 2008 Vary:accept-encoding
Fri 17:53:10 2008 content-length:104
Fri 17:53:10 2008 Content-type:text/html
Example one (with PHP's built-in compression function):
Copy Code code as follows:
? Php
if (extension_loaded (' zlib ')) Ob_start (' Ob_gzhandler ');
Header ("content-type:text/html");
?>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "/>
<title> Untitled Document </title>
<body>
<?php
For ($i =0 $i <10000; $i + +) {
Echo ' Hello world! ';
}
?>
</body>
? Php
if (extension_loaded (' zlib ')) Ob_end_flush ();
?>
Example two (self-write function):
Copy Code code as follows:
<?php Ob_start (' Ob_gzip ');?>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<meta http-equiv=" Content-type " Content= "text/html; charset=gb2312 "/>
<title> Untitled document </title>
<body>
</body>
<?php
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));
}
Return $content;
}
?>