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 this, and other content is output}?> 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, compress the content, and buffer the output ob_start (ob_gzhandler ); $ output =; For ($ I = 0; $ I <= max; $ I ++) {$ output. = This is line $ I;} The echo browser supports gzip compressed output; echo $ output;} else {// not supported by the browser, and direct output for ($ I = 0; $ I <= max; $ I ++) {$ output. = This is line $ I;} The echo 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 if you want more details, please refer to the mod_gzip project home page: http://sourceforge.net/projects/mod-gzip/ similarly, we can also use mod_deflate, compression ratio is slightly lower than mod_gzip. Calling the zip function consumes server memory, so use it with caution, depending on your needs. |