PHP program to accelerate the exploration of the compression output gzip

Source: Internet
Author: User
Tags ereg server memory
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.