PHP program accelerated exploration-compressed output gzip

Source: Internet
Author: User
Tags ereg
Using the mod_gzip module in Apache, we can use the gzip compression algorithm to compress the webpage content published by the Apache server and then transmit it to the browser of the client. If it is a plain text content, the effect is very obvious, it can be compressed to the original 30%-40%, so that the user's browsing speed is greatly accelerated. Gzip requires support from the client browser. Currently, most browsers gzip

Using the mod_gzip module in Apache, we can use the gzip compression algorithm to compress the webpage content published by the Apache server and then transmit it to the browser of the client. If it is a plain text content, the effect is very obvious, it can be compressed to the original 30%-40%, so that the user's browsing speed is greatly accelerated.

Gzip requires support from client browsers. most browsers currently support gzip, such as IE, Netscape, and Mozilla. Therefore, this method is worth a try. We can use the predefined variable $ _ SERVER ['http _ ACCEPT_ENCODING '] in PHP to determine whether the client browser supports gzip.

Gzip1.php

If (ereg ('gzip ', $ _ SERVER ['http _ ACCEPT_ENCODING']) {
// Browser support
} Else {
// The browser does not support output of other content
}
?>


Next, we will expand the above PHP program, use ob_start (ob_gzhandler) to compress the webpage content, store the content in the buffer, and send it to a browser that supports gzip. the browser will automatically decompress the compressed content, display.

Gzip2.php

Define ('Max ', 100 );

If (ereg ('gzip ', $ _ SERVER ['http _ ACCEPT_ENCODING'])
{
// The browser supports gzip to compress the content and buffer the output.
Ob_start ("ob_gzhandler ");
$ Output = '';

For ($ I = 0; $ I <= MAX; $ I)
{
$ Output. = "This is line $ I ";
}
Echo "the browser supports gzip compressed output ";
Echo $ output;
}
Else
{
// The browser does not support direct output.
For ($ I = 0; $ I <= MAX; $ I)
{
$ Output. = "This is line $ I ";
}

Echo "the browser does not support gzip compressed output ";
Echo $ output;
}
?>


The HTTP header information of a web page generated by using gzip compression is more like this than that of a general web page:

Content-Encoding: gzip
Content-Length: 270


For more details, see the mod_gzip project homepage:

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

Similarly, we can also use mod_deflate to lower the compression ratio than mod_gzip. Calling the zip function consumes server memory, so use it with caution, depending on your needs.

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.