This article mainly introduces two methods for enabling gzip compression in PHP. This article introduces the methods for enabling php configuration files and PHP files, for more information about how to enable gzip in PHP, see this article, for more information, see
After gzip compression is enabled, the size of the webpage can be reduced by 20% ~ 90%, which can save a lot of bandwidth, thus reducing the page response time and improving the user experience.
Php configuration change method:
The code is as follows:
Zlib. output_compression = On; enable the gzip function zlib. output_compression_level = 4; gzip compression level, 1 ~ 9. 3 ~ 5; zlib. output_handler =; gzip compression method. comments are recommended.
Php file modification method:
We recommend that you write it at the beginning of the page to a public file in the header.
The code is as follows:
If (! Headers_sent () & // if the page header information has not been output extension_loaded ("zlib ") & // and php has loaded the zlib extension strstr ($ _ SERVER ["HTTP_ACCEPT_ENCODING"], "gzip") // and the browser accepts GZIP {ini_set ('zlib. output_compression ', 'on'); ini_set ('zlib. output_compression_level ', '4 ');}