PHP based on Ob_start (Ob_gzhandler) To achieve Web page compression function method

Source: Internet
Author: User
This article mainly introduces PHP based on Ob_start (' Ob_gzhandler ') implementation of the Web page compression function, involving PHP ob_gzip, Ob_start and other functions such as buffer and content compression related skills, the need for friends can refer to the next

In this paper, we describe the method of Php ob_start (' Ob_gzhandler ') to implement the Web page compression function. Share to everyone for your reference, as follows:

PHP generated after the page to the browser display, the opening speed of the page in addition to the user's Internet connection, often also with the size of the page is very relevant, we can start from the size of the page to improve the response speed of the page.

The following code is an example of a compressed Web page, and we use the Ob_gzip function to compress the output to "buffer" and then output it using Ob_start.

PHP code

Enable compression if (function_exists (' Ob_gzip ')) {Ob_start (' Ob_gzip ');} Prepare some content for compression for ($i =0; $i <100; $i + +) {echo (' Here is test content <br> ');} Output compression results 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);}

How much it works, here I tested the code above.

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.