Content Compression Using PHP
|Average rating: Rate this article|
HTTP 1.0 introduced the idea of content encodings. A browser/client can notify the server that it can accept compressed content by sending the Accept-Encoding header. The Accept-Encoding header can be set as follows
or with just one of gzip or deflate. You don't need to worry about any of this - PHP will automatically choose the correct compression to use.
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. Probably best to use a midrange value like 5. Or you can test different values and compare the content compressions. whatsmyip.org/mod_gzip_test shows you the original and compressed content sizes for any page you submit. It also tells you if your content is compressed - good for checking if your settings are working.
The PHP manual recommends using this method over the next method.
Note: You can only enable zlib.output_compression in your php.ini file, even though the PHP documentation suggests otherwise.
The disadvantage with zlib is you need access to the php.ini file, if zlib.output_compression is not already enabled (phpinfo() can show you if its enabled). Some ISPs do not give you access to the php.ini file.
You can use PHP's output buffering to compress your pages. In your scripts, or on the page you want compressed, add the following line
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 output in PHP's output buffer. At the end of the script, the function ob_gzhandler is called. This callback function determines the compression to use ('deflate' or 'gzip'), and compresses the content. 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 68% using these methods. Not bad for one line of code!
For more on the Accept-Encoding header, follow the link.
Print this article Bookmark:
About The Author
Rate This Article
Use your mouse pointer to select as many stars as you want, and press the left mouse button to vote.
Other PHP Articles
Rating: 4.6 stars
|Some Top Tips from the Best SEO's by Michael Biehn (Feb 6, 2007)|
|Know what you want to be found for. You should know the top two or three terms that are most important to your Web site and have incorporated them into a 25-word description that doesn't use marketing hype, which can then be submitted to human powered directories...|
Rating: 4.7 stars
|43 Tips for Optimizing PHP code by Reinhold Weber (Oct 18, 2007)|
|Here is the list of 43 short tips you can use for writing an optimized and more efficient PHP code.. ..|
Rating: 4.1 stars
|Auto Optimize Your MySQL Tables Script by John Miller (May 25, 2008)|
|In my quest to make our clients MySQL driven ecommerce websites running fast, I've pieced together a script and cron job that will save you some support calls down the road....|
Rating: 4 stars
|3 Simple Ways to restrict access to your webpages using PHP by wmtips.com (Nov 21, 2006)|
|Why do you need to restrict access to some of your scripts or webpages? There are can be several reasons to do this: You can use some open-source php script (for example, statistics frontend), and you can not be fully assured that your data completely safe...|
Rating: 4.5 stars
|Regular expressions made easy by wmtips.com (Nov 19, 2006)|
|Regular expressions is a very powerful instrument to manipulate and extract strings. However not all PHP developers know how to use regular expressions, so this simple tutorial is intended to everyone who wants to learn them...|