How To Compress Your Pages With GZIP And PHP

Recently, we wrote an article which outlined 32 things that webmasters can do to speed up their websites. One item that we mentioned was the use of GZIP compression to speed up the delivery of web pages. Most people, however, hadn't heard of GZIP, so we were contacted for methods to add it to websites.

In this article, we show how to use GZIP with PHP on a Linux Apache web server.

Does Your Web Host Provide GZIP Support?

Most web hosts compile PHP with support for GZIP. In fact, we have yet to find a Linux web host which does not support GZIP compression.

If you are in doubt about your web host, simply contact them and inquire about GZIP.

How To Add It To Your Website

Adding GZIP to your website is very simple. In fact, all you have to do is add one simply line of code to your php pages.

The code snippet below needs to be placed as the first line in your document, before any other code.

<?php ob_start("ob_gzhandler"); ?>

And that's it. Now your web pages will be served more quickly.

Skeptical?

You might be wondering how that little line of code can make your web pages more efficient. And given that it is a PHP function, you might wonder why the added processing time does not affect the page load. However, GZIP is proven effective.

If you are skeptical, consider using a service such as WebOptimization.com to compare your load time before and after compression. Another useful tool is GTmetrix.

 

More information about GZIP can be found in the PHP.net manual.

Tags: , ,

10 Responses to “How To Compress Your Pages With GZIP And PHP”

  • Jim May 29, 2010 at 10:20 pm

    Do you mean we have to place this code <?php ob_start(“ob_gzhandler”); ?> on the first line on all our php files, for Wordpreass on index.php, header.php, page.php, single.php, footer.php, sidebar.php, author.php, 404.php?

  • andy December 3, 2010 at 6:24 pm

    Hey there, thanks so SO! much for your help with all of these posts, they are amazing. I’ve copied and pasted the code into my own code, and it works like an absolute charm… Problem is though, it edits all of the font settings and a little of the layout of my site, which isn’t great. Any thoughts? Many thanks!

  • angela July 27, 2011 at 10:13 am

    this worked wonders on 2 of my client’s websites but it showed a blank white page on 1 other site. All 3 sites are wordrpess but I’m wondering if it has to do with how the theme is coded on the one that failed. Any ideas? Thank you for this tip though it worked wonders on the other 2!

    • Velvet Blues July 28, 2011 at 6:08 am

      I haven’t seen the white page… If the theme works fine without being compressed, I suspect that this could be something to do with the hosting configuration.

  • Freddyjr January 25, 2012 at 7:24 pm

    I haven’t done this yet,I’m currently trying out W3 Cache for wordpress but if I see no improvement I’d give this a try! Thanks for sharing these great tips!

    Great site! Thanks!

  • AfroMerry August 29, 2012 at 7:28 am

    This is interesting! I am going to give it a try to see if it will make a difference. Thanks for sharing this wonderful piece. A slow website is bad!

  • Jacqui October 9, 2012 at 3:25 pm

    Genius!!!! Thank you so much for such a simple instruction. I tried editing the .htaccess file and messing with Apache handlers but nothing worked…until this. I am using a WordPress site so I just placed the code at the very top of my header.php and viola!
    I then tested the site for compression here
    http://www.gidnetwork.com/tools/gzip-test.php
    (Previous tests showed up as no compression)

  • Rajendra Khope November 19, 2012 at 6:11 am

    Put this in gzip.php

    if (substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip')) ob_start("ob_gzhandler"); else ob_start();

    include gzip.php on each page

  • Jen Smoody January 9, 2013 at 11:53 am

    Does this work on .html or only php? I built my own site but have a lot of gaps in knowledge about site building. I have a very photo heavy site – I’ve compressed all of them for web in photoshop and it still loads slowly. I’m searching for a solution to speed it up.
    Thanks for any help.

Trackbacks

Trackback URL:

Leave a Reply

Want us to work on your project?

Contact us today for a quote. Click here to submit details regarding your project.

If you are making a general inquiry, send an email to info@velvetblues.com

Go Daddy Deal of the Week: $2.95 .COM Domains! Offer expires 11/20/12!