To achieve GZIP compression page requires browser and server co-support, in fact, the server compression, uploaded to the browser after the browser decompression and parsing. There is no need for us to worry about the browser because most browsers now support the parsing of gzip-over pages. We just have to compress the page on the server side and then output it to the browser.
A little wordy, here are the things to say:
As to make compressed biscuits, first to get the raw material, to compress a page, first to obtain the content to output. PHP Ob_start () (ob = output buffer) function can implement this function, it can put the contents of the program to prepare the output to a place called "buffer", of course, you can understand for the production of compressed biscuits temporarily put the raw material of the work station.
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 prepare it before the arrival of the raw materials, otherwise the raw materials come to no place, there will be problems. With Ob_start () to get the page to compress, we can make compressed biscuits, 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 extension to do one:
Copy CodeThe code is as follows:
function Ob_gzip ($content)//$content is the content of the page to be compressed, or the biscuit material
{
if (!headers_sent () &&//If the page header information is not output
extension_loaded ("zlib") &&//and zlib extension has been loaded into PHP
Strstr ($_server["http_accept_encoding"], "gzip")//and the browser says it can accept the gzip page
{
$content = Gzencode ($content. "\n//This page is compressed", 9); Paste the "//This page compressed" comment label on the contents of the compressed content, and then use the Gzencode () function provided by zlib to perform a level 9 compression, which ranges from 0-9,0 to no compression, 9 for maximum compression, and, of course, the higher the compression level, the more CPU.
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 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, just add a parameter to Ob_start (), and the parameter name is the function name of the "compressor" we just made. So when the content enters the buffer, PHP calls the Ob_gzip function to compress it.
Well, all the work has been done and the final delivery:
Ob_end_flush (); End buffer, output content. Of course, this function is not necessary, because the program executes to the end automatically outputs the buffer contents.
The complete example is as follows:
Copy CodeThe code is as follows:
Enable a workbench with a ob_gzip compressor
Ob_start (' Ob_gzip ');
Prepare some content to compress
for ($i =0; $i <100; $i)
{
Echo (' Here is the raw material for compressing the biscuit, here is the raw material of the compressed biscuit, raw material ');
}
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 is 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, enable gzip reduced to 104B, uh ... I may not be good at maths, I calculate the amount of compression 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 CodeThe code is as follows:
Fri Jan 17:53:10 http/1.1
Fri Jan 17:53:10 server:microsoft-iis/5.1
Fri 17:53:10-Date:fri, Jan 09:53:10 GMT
Fri Jan 17:53:10 Connection:close
Fri Jan 17:53:10 x-powered-by:php/5.2.5
Fri Jan 17:53:10 Content-encoding:gzip
Fri Jan 17:53:10 vary:accept-encoding
Fri Jan 17:53:10 content-length:104
Fri Jan 17:53:10 content-type:text/html
Example one (with PHP's built-in compression function):
Copy CodeThe code is as follows:
if (extension_loaded (' zlib ')) Ob_start (' Ob_gzhandler ');
Header ("content-type:text/html");
?>
<title>Untitled Document</title>
for ($i =0; $i <10000; $i + +) {
Echo ' Hello world! ';
}
?>
if (extension_loaded (' zlib ')) Ob_end_flush ();
?>
Example two (self-write function):
Copy CodeThe code is as follows:
<title>Untitled Document</title>
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;
}
?>
http://www.bkjia.com/PHPjc/321325.html www.bkjia.com true http://www.bkjia.com/PHPjc/321325.html techarticle to achieve GZIP compression page requires browser and server co-support, in fact, the server compression, uploaded to the browser after the browser decompression and parsing. There is no need for us to worry about the browser .