This article mainly introduces two methods for enabling gzip compression in PHP. This article introduces the php configuration file and enabling method and PHP file enabling method. For more information, see
This article mainly introduces two methods for enabling gzip compression in PHP. This article introduces the php configuration file and enabling method and PHP file enabling method. 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") & // 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 ');
}