Using the mod_gzip module in Apache, we can use the gzip compression algorithm to compress the web page content published by the Apache server and then transmit it to the client's browser. If it is pure text content, the effect is very obvious, and it can be compressed to about 30%-40% of the original size, greatly speeding up the user's browsing speed.
Gzip requires client browser support. Currently, most browsers support gzip, such as IE, Netscape, Mozilla, etc., so 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])) {
} else { //The browser does not support it, output other content } ?> |
define(MAX,100); if(ereg(gzip,$_SERVER[HTTP_ACCEPT_ENCODING])) { //浏览器支持gzip,将内容压缩并缓冲输出 ob_start("ob_gzhandler"); $output = ; for($i=0;$i<=MAX;$i++) { $output .= "This is line $i "; } echo "浏览器支持gzip压缩输出"; echo $output; } else { //浏览器不支持,直接输出 for($i=0;$i<=MAX;$i++) { $output .= "This is line $i "; } echo "浏览器不支持gzip压缩输出 "; echo $output; } ?> |
define(MAX,100);
if(ereg(gzip,$_SERVER[HTTP_ACCEPT_ENCODING])) { //The browser supports gzip, which compresses the content and buffers the output ob_start("ob_gzhandler"); $output = ; for($i=0;$i<=MAX;$i++) echo "The browser does not support gzip compressed output"; echo $output;
}
?> |
Content-Encoding: gzip Content-Length: 270 |