Save Bandwidth – Enable GZip Compression In PHP

Mattias Geniar, Monday, December 22, 2008 - last modified: Saturday, August 14, 2010

GZip is a formidable way to save up on bandwidth, without hindering your users. It allows any script to pass its content by the compression tool first, before being sent out. The receiver will then receive the compressed file, and unpack it to show its content.

Browsers that don't support GZip will automatically fall back to the normal way of sending out content: unzipped.

It's a win-win situation. You save bandwidth, and don't bother your users. So, let's enable it.

The easiest is way is by using a .htaccess file. Add the following line to it.

php_value output_handler ob_gzhandler

That simple line will reduce the bandwidth used by plain text files (html, css, javascript, rss, ...) by as much as 80%. It doesn't do much for images (JPG/PNG is a formidable compression on its own, and can't be compressed much further), but it's a saver for anything that isn't image/video.

Since not every host allows .htaccess to overwrite certain system settings, you can also define this in your PHP pages itself, as described on the following page: Other Methods to Enable GZip Compression.

You can quickly test the result of your compression, at the following website: compression / deflate / gzip test tool.

There 's a (relatively) small downside to this: it requires a bit of CPU power from your webserver to compress the page, and a bit of CPU power from your visitor to decompress the page. This probably won't be an issue, unless you have a very busy server.


Hi! My name is Mattias Geniar. I'm a Support Manager at Nucleus Hosting in Belgium, a general web geek, public speaker and podcaster. If you're interested in keeping up with me, have a look at my podcast and weekly newsletter below. For more updates, follow me on Twitter as @mattiasgeniar.

SysCast podcast

In the SysCast podcast I talk about Linux & open source projects, interview sysadmins or developers and discuss web-related technologies. A show by and for geeks!

cron.weekly newsletter

A weekly newsletter - delivered every Sunday - for Linux sysadmins and open source users. It helps keeps you informed about open source projects, Linux guides & tutorials and the latest news.

Share this post

Did you like this post? Will you help me share it on social media? Thanks!

Comments

lulz Monday, December 22, 2008 at 22:46

php, you say? i luled.

Reply


Matti Tuesday, December 23, 2008 at 01:28

It uses the Output Buffering built into PHP, to compress the pages before output. How’s that not PHP mister “I luled”?

Reply


stelian Monday, December 29, 2008 at 15:29

I know this is not the right place to write this, but it would be nice if I could see the date on the first page for every post.

Reply


Matti Monday, December 29, 2008 at 19:29

You’re quite right, it’s been added to the frontpage. :-)

Reply


tuto Wednesday, May 13, 2009 at 07:22

good post..thanks

Reply


bashir Thursday, December 1, 2011 at 14:48

You’re quite right

Reply


Leave a Reply

Your email address will not be published. Required fields are marked *

Inbound links