Content Compression Using PHP
HTTP 1.0 introduced the idea of content encodings. The Accept-Encoding request-header was expanded on in HTTP 1.1, enabling a browser/client to notify the server that it can accept compressed content by sending the header. The Accept-Encoding header can be set as follows:
or with just one of gzip or deflate. PHP will automatically choose the correct compression to use. Gzip is by far the most popular and widely used compression method. For Apache 2.x, gzip compression is handled by the mod_deflate module.
Compressing content usually results in less data being send across the network. This has three benefits:
- Bandwidth usage decreases - you can serve more visitors before you reach your bandwidth limit
- Network latency can be reduced because fewer packets will be used to transmit the data. Your application can appear faster to a user - page load times are reduced.
- compressing files allows you to format your xhtml code for readibility without sacrificing page load times.
There are two methods to using compression in PHP:
Your PHP needs to be built with the zlib extension. In your php.ini file set
zlib.output_compression = On
to enable compression. You also need to make sure zlib.output_handler is empty.
You can change the level of compression by using
zlib.output_compression_level = d
where d is a digit from 1 (minimal compression) to 9 (maximum compression). The higher compression levels increase CPU usage, possibly without any significant reduction in content size. It's probably best to use a mid-range value like 5. You can also set enable and set compression levels using the ini_set php function:
ini_set('zlib.output_compression', 1); //turn on compression
ini_set('zlib.output_compression_level', 5); //set compression level to 5
You can use different values and compare the content compressions. HTTP Compression Test shows you the original and compressed content sizes for any page you submit. It also tells you if your content is being compressed - good for checking if your settings are working.
The PHP manual recommends using this method over the next method.
The alternative to zlib is to use PHP's output buffering to compress your pages. In your scripts, or on the page you want compressed, add the following line
<?php ob_start('ob_gzhandler'); ?>
This should be at the very top of your scripts. It needs to be called before any content is send to the browser/client. If you have an include file that you use for all your pages, you might want to put this line at the top of that file.
This will put all the script's output into PHP's output buffer. At the end of the script, the function ob_gzhandler is called. This callback function determines the compression type accepted by the browser/client, and compresses the content accordingly. The compressed content is then send to the browser/client.
You have seen two methods of compressing content using PHP with very little effort on your part. I have seen page sizes reduced by up to 80% using these methods. Not bad for one line of code!