Web Design and Development
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.
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.
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.
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: compression software, gzip, load timeTrackback URL:
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
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?
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!
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!
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.
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!
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!
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)
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
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.
@Jen: This only works for pages which support PHP. HTML files do not natively support PHP.