How to compress gzip pages using PHP

Source: Internet
Author: User
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;
}
?>

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.