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: