Two PHP methods are used to compress gzip pages. However, the premise of the two methods is that the server must support gzip compression. if the server does not support gzip, the two methods are useless. The following describes how to use php to compress gzip pages. Method 1 (using php's built-in compression function): & lt ;? PHPif (Extension_Loaded (zlib) Ob_Start (ob_gzhandler); Header (& quot; Content-t PHP) two methods to achieve gzip page compression, however, the premise of the two methods is that the server must support gzip compression. if the server does not support gzip, the two methods are useless.
The following describes how to use php to compress gzip pages.
Method 1 (using php's built-in compression function ):
if(Extension_Loaded('zlib')) Ob_Start('ob_gzhandler');
Header("Content-type: text/html");
?>
scutephp
for($i=0;$i<10000;$i++){
echo 'Hello World!';
}
?>
if(Extension_Loaded('zlib')) Ob_End_Flush();
?>
Method 2 (self-writing function ):
Scutephp
Ob_end_flush ();
// Compression function
Function ob_gzip ($ content ){
If (! Headers_sent () & extension_loaded ("zlib") & strstr ($ _ SERVER ["HTTP_ACCEPT_ENCODING"], "gzip ")){
$ Content = gzencode ($ content, 9 );
Header ("Content-Encoding: gzip ");
Header ("Vary: Accept-Encoding ");
Header ("Content-Length:". strlen ($ content ));
}
Return $ content;
}
?>