PHP program accelerated exploration of compressed output gzip_php tutorial

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 Web content published by the Apache server before it is transferred to the client's browser. If the content is plain text, the effect is very obvious, approximately can be compressed to the original 30%-40%, so that the user's browsing speed greatly accelerated.

Gzip requires client browser support, most browsers currently support gzip, such as Ie,netscape,mozilla, so this method is worth a try. We can use the predefined variables $_server[' http_accept_encoding ' in PHP to determine if 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 extend 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]))
{
The browser supports gzip, compressing content and buffering 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 that is generated by using gzip compression will have more information than a typical Web page:

Content-encoding:gzip
content-length:270

If you would like more detailed information, please refer to the Mod_gzip Project homepage:

http://sourceforge.net/projects/mod-gzip/

Similarly, we can also use mod_deflate, the compression rate is slightly lower than the mod_gzip. Calling the ZIP function requires server memory, so use it sparingly, depending on your needs.

http://www.bkjia.com/PHPjc/508468.html www.bkjia.com true http://www.bkjia.com/PHPjc/508468.html techarticle using the Mod_gzip module in Apache, we can use the GZIP compression algorithm to compress the Web content published by the Apache server before it is transferred to the client's browser. If it is plain text ...

  • 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.