#GZIP compression is a simple and effective way to save bandwidth and speed up PHP applications. The mechanism working behind GZIP compression is described below -
The browser/client requests the file from the server.
The server sends the browser a .zip file (index.html.zip) in response instead of plain old index.html, so download time and bandwidth are reduced.
After executing the above steps, the browser will download the compressed file, decompress it, and then display it to the user. This loads web pages very quickly.
In the Apache server we have to add the following to the .htaccess file to enable GZIP compression.
# compress text, html, javascript, css, xml: AddOutputFilterByType DEFLATE text/plain AddOutputFilterByType DEFLATE text/html AddOutputFilterByType DEFLATE text/xmlin AddOutputFilterByType DEFLATE text/css AddOutputFilterByType DEFLATE application/xml AddOutputFilterByType DEFLATE application/xhtml+xml AddOutputFilterByType DEFLATE application/rss+xml AddOutputFilterByType DEFLATE application/javascript AddOutputFilterByType DEFLATE application/x-javascript # Or, compress certain file types by extension: <files *.html> SetOutputFilter DEFLATE </files>
In PHP files, we can enable GZIP compression.
<?php if (substr_count($_SERVER[‘HTTP_ACCEPT_ENCODING’], ‘gzip’)) ob_start(“ob_gzhandler”); else ob_start(); ?>
The above is the detailed content of How to enable GZIP compression in PHP?. For more information, please follow other related articles on the PHP Chinese website!