How to use gzip compression on your PHP websites

- Apache, PHP -


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.


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.


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:


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

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);


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.


Embedding YouTube Movies in XHTML

- Tutorial, xhtml -

YouTube If you try to embed a YouTube-video in a XHTML 1.0 compliant website, you will get validations errors. This is caused by YouTube’s use of the <embed> tag, which has been deprecated in XHTML 1.0.

The standard YouTube code is:

To solve the validations errors we have to get rid of the deprecated <embed> tag.

Therefore we add a data attribute to the <object> tag. The following code is the most simple and will work in Opera and Firefox:

To get it working in MS Explorer we need to add a <param> tag with name and value attribute:

To autoplay a movie simply add “&autoplay=1” to the url:

Easy isn’t it? The code in xhtml is even shorter than the standard YouTube code.


Parsing newsfeeds with XSL (1)


Magpie is a very popular RSS parser for PHP. Although I used Magpie quite a while to my satisfaction, it suffers a few flaws. At first it seems to have problems with the UTF8. Dealing with UTF8 is not easy in PHP (and MySQL). And there is verbose code, you feel it can be done quicker and simplier. We will parse feeds with only 5 lines of code and a simple stylesheet.
Read the rest of this entry »


Localizing Ubuntu

- Linux -

Ubuntu is a Debian based Linux distribution. In its few years of existence it made quite an impression by its support and speed of development. If offers the advantage to Debian in the way it supports the modern and rare hardware. I had no problem installing it on a new Dell right out of the box. I use the server version of Dapper Drake 6.06 on my PHP server. Once you have installed the OS, you can run into problems localizing your system. I wanted to have Dutch dates on my Dutch website and English dates on my international sites. The way to add support for different languages differs quite a bit from earlier Ubuntu versions, because ‘dpkg-reconfigure locales’ doesn’t work. But once you know the secret is not difficult at all.
Read the rest of this entry »