Use GZip with PHP

Posted October 14th, 2011 • 2 min read

I recently started using GZip headers in my websites and the results are simply amazing. Right up there with coffee, sneezing polar bears and green traffic lights.

Nowadays, files are big. People used to optimize graphics and CSS stylesheets. This day and age we just don't care anymore. At the same time bandwidth is getting more expensive and the mobile market is growing bigger. Not a good combination.

Enter GZip

Adding GZip to your applications couldn't be simpler, and using this compression to your output can reduce the amount of data being sent by around 70-80% for your average stylesheets. That's what you call weight-loss.

I recently built the backend of a mobile application, which relied on a JSON interface. Data being sent to the phone was around 250kb. Optimizing the content, only returning the bare minimum the phone needed to work resulted in the file being 197kb. So, I added the GZip compression and guess what. The resulting file was 14kb. Awesome.

So how do I use it

Simple. At the top of your PHP file you add the following:

@ob_start ('ob_gzhandler');
header('Content-type: text/html; charset: UTF-8');
header('Cache-Control: must-revalidate');
header("Expires: " . gmdate('D, d M Y H:i:s', time() - 1) . ' GMT');
?>

This will tell the server to first zip the contents, before sending it back to the client, where it is deflated.

The only caveat is that you must have mod_gzip installed as an Apache module, but most hosting providers install this by default. Just make sure yours does too.

As a CakePHP component

Jose Gonzales made a nice little plugin to use GZip in your Cake Applications. Find it at Github Basically, all you have to do is add the plugin to your plugins folder, and then add the following to your app_controller.php

var $components = array('Gzip.Gzip');

And you're good to go.

Enjoy!

Stay up to date

Want to know when a new post comes out and stay in the loop on tips, tricks and gotchas? Consider signing up for the Mindthecode newsletter.

Comments

Keep reading

May 26th, 2011 • 1 min read
In this short post I will show you how to get rid of a Git submodule
December 6th, 2011 • 2 min read
Loading external assets with curl is not only easy, it's also a lot faster than file_get_contents
May 25th, 2014 • 5 min read
When I realised you could easily generate screenshots from a site with PhantomJS I just needed to know if I could take it one step further, and record a video.