Compress PHP Output And Save On Bandwidth!
We all love web pages that load super fast and as Google pushes us to "make the web faster," we must heed the call.
In my search for ways to increase efficiency while decreasing page load times and bandwidth output, I happened upon the wonderful world of Zlib compression. This compression type uses gzip to compress the output of your files and feed them to the browser.
How cool is that?
While it might sound like it would be an impossible task, it's really as simple as doing two things-
Step 1. Make sure your PHP server has the zlib extension
All you have to do is create a file called something like phpinfo.php on your server and put the following code inside:
<?php phpinfo(); ?>
Once it loads, just find 'zlib' and check to see whether it's there and whether it's enabled. If not, you need to either enable it yourself or ask your server admin to do it.
Step 2. Pop in the code!
Put the following code at the very top of any pages you want compressed, or (preferably) in your header file for the entire site-
<?php
ini_set('zlib.output_compression', 'On');
ini_set('zlib.output_compression_level', '1');
?>
OR if that doesn't work for you, you can try this-
<?php
ob_start('ob_gzhandler');
?>
...that's it!
Now try reloading your page.
If everything went right, you should notice an improvement in load time and where you'll notice it most is in your bandwidth usage at the end of the month.
I tested the before and after on Sitdiary.net and the file got compressed by over 3X every time! In other words, I'm going to cut my bandwidth usage down by 2/3 on all PHP files.
*** UPDATE ***
On my WordPress blogs, I've been testing this out on the index.php and it seems I'm getting upwards of 75% compression rates!
July 3rd, 2009 - 02:36
Sorry, but using both zlib.output_compression and ob_gzhandler() is nonsense. You can only use one of the two methods as they’ll interfere with each other. Using zlib.output_compression where available is preferred over ob_gzhandler(). Please see http://de3.php.net/manual/en/zlib.configuration.php#ini.zlib.output-compression and http://de3.php.net/ob_gzhandler.
July 3rd, 2009 - 04:47
mod_deflate is faster in my opinion…
July 3rd, 2009 - 11:13
@Stefan: You’re absolutely right. I should have separated them out like they are now. Thanks for your input.
@Gyorgy: I’ll give that a shot.
October 15th, 2009 - 09:27
Hi Scott,
I linked to your post: http://bradblogging.com/tutorials/speed-wordpress-up-3-times-as-fast-under-a-minute/
Surprised that it didn’t appear as a trackback.
November 13th, 2009 - 05:38
Damn! I just typed a whole long comment and then the screen went funny when I hit the submit button. Is it in moderation or do i have to type it all out again?
November 13th, 2009 - 10:31
Altech, I don’t see it in moderation, sorry.
December 30th, 2009 - 15:45
Hi Scott
Interesting… does the compression cause any problems in IE6 browsers?
I’ve been told that gzip compression can cause problems. What about zlib?
January 13th, 2010 - 12:49
Keith,
I’m not really sure. I haven’t had any complaints from IE6 users on my “compressed” sites, so I’m inclined to say no.
If you hear anything, let me know.
Scott
January 16th, 2010 - 06:08
Added:
1.
to the top of my header.php file.
It worked but prevented All In One SEO Pack from functioning.
Removed it and everything went back to normal.
Please let me knowby email if you can throw any light on this.
Thank you very much.