Php implements the web page compression function based on ob_start (ob_gzhandler), obgzhandler

Source: Internet
Author: User

Php implements the web page compression function based on ob_start (ob_gzhandler), obgzhandler

This example describes how php compresses Web Pages Based on ob_start ('ob _ gzhandler. We will share this with you for your reference. The details are as follows:

After a Web page is generated by PHP, it is sent to the browser for display. The page opening speed is not only related to the user's network speed, but also closely related to the page size. We can start with the page size, to improve the response speed of the web page.

The following code is an example of compressing a webpage. We use the ob_gzip function to compress the output content with ob_start and put it in the "buffer zone" before outputting the content.

PHP code

// Enable compression if (function_exists ('ob _ gzip ') {ob_start ('ob _ gzip ');} // prepare some content to be compressed for ($ I = 0; $ I <100; $ I ++) {echo ('test content <br> ');} // output the compression result ob_end_flush (); // This is the ob_gzip compression 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 );}

In the end, I tested the above code here.

Before compression:

After compression:

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.