GZIP (Gnu-zip) is a compression technique. The page size can be turned into 30% or smaller after gzip compression. This way the user will feel very happy when browsing!
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:
Code:
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
Code:
ob_start('ob_gzip'); //没错,就是给ob_start()加一个参数,参数名就是我们刚才做的“压缩机”的函数名。这样当内容进入缓冲区后PHP就会调用ob_gzip函数把它压缩了。
Well, all the work has been done, the final delivery:
Code:
ob_end_flush(); //结束缓冲区,输出内容。当然,不用这个函数也行,因为程序执行到最后会自动将缓冲区内容输出。
The complete example is as follows:
Code:
<?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.