Using the Mod_gzip module in Apache, we can use the GZIP compression algorithm to compress the content of the Web page published by the Apache server and then transfer it to the client's browser. If the content is plain text, the effect is very obvious, can be compressed to the original 30%-40%, so that the user's browsing speed greatly accelerated.
Gzip requires client browser support, most browsers now support gzip, such as Ie,netscape,mozilla, so this method is worth a try. We can use predefined variables in PHP $_server[' http_accept_encoding ' to determine whether the client browser supports gzip.
gzip1.php
if (Ereg (' gzip ', $_server[' http_accept_encoding ')) {
Browser support
} else {
Browser not supported, output other content
}
? >
Next we expand on the above PHP program, using Ob_start (Ob_gzhandler) to compress the content of the Web page, buffer and send to the browser to support gzip, the browser will automatically extract the compressed content, display.
gzip2.php
Define (' MAX ', 100);
if (Ereg (' gzip ', $_server[' http_accept_encoding '))
{
Browsers support gzip, compress content and buffer output
Ob_start ("Ob_gzhandler");
$output = ';
for ($i =0; $i <=MAX; $i)
{
$output. = "This was line $i";
}
echo "browser supports gzip compression output";
Echo $output;
}
Else
{
Browser not supported, direct output
for ($i =0; $i <=MAX; $i)
{
$output. = "This was line $i";
}
echo "Browser does not support gzip compression output";
Echo $output;
}
? >
The HTTP header information for a Web page generated by using gzip compression is more information than a typical Web page:
Content-encoding:gzip
content-length:270
If you would like to have more detailed information, please refer to the Mod_gzip Project homepage:
http://sourceforge.net/projects/mod-gzip/
Similarly, we can use mod_deflate, the compression rate is slightly lower than mod_gzip. Calling the ZIP function consumes server memory, so use caution, depending on your requirements.