How to use gzip compression on your PHP websites

Why?

Compressing weboutput can save valuable bandwidth. The other advantage is that clients will load your websites faster. Gzip compression is supported by all modern browsers, so why not use it. It will compress your pages between 60% and 80%. So you will lower your bandwidth-costs by an average of 70%. Also your server can push more requests in case you have limited connection speed. Of course it will not work for images.

Apache

Your (Apache)server can do the job, but only when it is configured for it. There was a Apache 1 Module called mod_gzip that compresses the HTML as it sends it out. For Apache 2 this module is called mod_deflate and it also compresses content before it is delivered to the client.

PHP

If you have no control over the Apache config files there is another way to solve the problem. Let PHP compress the content. If you have PHP > 4.3 you can use the zlib.output_compression.

It’s simple, change this setting to ON in your PHP ini file:
zlib.output_compression = On

That’s all. Every page will now send compressed content to every browser that understands the compression method.

NOTE: Don’t forget to disable gzip-compression in CMS-packages like Joomla in the administration section, otherwise you will spoil not only CPU resources but also you can run into trouble with the double compressed content that will show in some browsers only strange unreadable icons on the screen.

You can also adjust the compression level by setting this between 1 and 9:
zlib.output_compression_level = 6

Make your own choice between CPU time to compress on a higher level and the less data that will be sent.

Compression test

I ran a test, without gzip the content of my page is 20162 bytes. Now with different levels of compression, I came to the following results:

  1. 6779 bytes
  2. 6690 bytes
  3. 6591 bytes
  4. 6264 bytes
  5. 6147 bytes
  6. 6134 bytes
  7. 6127 bytes
  8. 6127 bytes
  9. 6125 bytes

I reduced the load of my webpage with 70%. The default value is 6, which is in my test the highest setting with some noticeable compression gains. So their is no real need for setting the level at all in the PHP ini:

zlib.output_compression_level

Just delete the line, if it’s there, PHP info will show -1 which is fine, because the default compression level is 6.

Measuring CPU load is nearly impossible and should not be considered to be an issue until you run into problems with that. In most cases bandwidth is limited and CPU time is plentiful.

Alternative method 1

There was another way of reaching the same by puting this at the start of every PHP-script:
< ? ob_start("ob_gzhandler"); ?>

This still works in PHP 5, but according to the manual is not recommended.

Note: You cannot use both ob_gzhandler() and zlib.output_compression. Also note that using zlib.output_compression is preferred over ob_gzhandler().

Alternative method 2

If you have no access to your PHP.ini file you can set the option in the .htaccess file in the root directory of your website.

php_flag zlib.output_compression on
CSS

When CSS is embedded it’s automatically compressed as well. For enabling CSS files to be compressed, simply rename your style.css files to style.php and it’s done automatically. Of course not forget to refer from your html to the new name when linking.

When you don’t want to adjust the HTML, or for use with WordPress or Joomla that work with different templates that are automatically referred to from the code, the following solution will work and it’s simple again:

  1. Copy yourstylesheet.css to yourstylesheet.php
  2. Replace the content of yourstylesheet.css to

    @import url(yourstylesheet.php);

Done!

Edited 06-12-12:
For Mozilla/Firefox you need to add

< ? header("Content-type: text/css; charset: UTF-8"); ?>

to yourstylesheet.php at the start of the page. Otherwise the CSS is not recognised as CSS. In Opera/MSIE it was working correctly.

3 Responses to “How to use gzip compression on your PHP websites”

  1. Josh Says:

    Thanks– that php_flag option saved my butt on a host that doesn’t enable the deflate or gzip modules.

  2. dermacai Says:

    Nice article. I really liked it. Can I have your e-mail address, so I can ask you a few things?

  3. Yogi Raj Says:

    Instructions are clear and easy to follow. Thanks for the details info.

Categories
Archives
Links