Elliott C. Back: In Aere Aedificare

Http Gzip Compression in PHP

Posted in Blogging, How to Blog, Computers & Technology, Spread IE, SEO, Web 2.0, Performance by Elliott Back on January 12th, 2006.

GZIP compression saves as much as 70% of the size on a text-heavy webpage. What does this mean for your users? Faster loading websites and less latency. What does this mean for your server? Less load and less bandwidth will be used if you judiciously compress and cache.

In PHP, there are two easy ways to do this. The first is to add the following to your .htaccess file and let Apache do the rest:

php_flag zlib.output_compression on
php_value zlib.output_compression_level 2

The second is to add the following to the top of your php script file:

ob_start(”ob_gzhandler”);

Either way, you can check the results of your page online.

This entry was posted on Thursday, January 12th, 2006 at 10:54 pm and is tagged with output compression, compression level, gzip compression, script file, php script, htaccess, bandwidth, apache. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback.

4 Responses to 'Http Gzip Compression in PHP'

  1. Max said:

    on February 19th, 2007 at 1:14 pm

    Thanks! It worked just fine for me

  2. Dustin said:

    on November 15th, 2007 at 9:05 am

    “What does this mean for your users? Faster loading websites and less latency.”

    Just wanted to note that this really depends - while the file will obviously transfer faster, being much smaller in size and taking less bandwidth, often it will actually take longer for the page to load as the content must be decompressed after transfer. More info is available on php.net.

  3. Andreas said:

    on March 20th, 2008 at 12:49 pm

    Thanks!

    Damn this is good.

    It reduced the page size from 9 k to 2 k.

  4. vishal said:

    on June 14th, 2008 at 4:44 pm

    @Dustin :

    You probably have not been through details.

    Unless you have computer of 1970s, decompressing wont take any time (i mean negligible). But bandwidth and network technology can’t compete with the CPU power.

    Conclusion:
    Its always better to use gzip, unless you expect very old version browsers to be used.

Your Thoughts Go Here:

Powered by WP Hashcash