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: