Enabling Gzip Compression

Posted by in Code Snippets

<!--?<?php
 ob_start('ob_gzhandler');
?>

Just place this piece of code at the top in your PHP page and you should see a significant performance improvement. Gzip Compression is by far, the most popular and efficient method when it comes to compressing data sent by web servers to browsers. This method claims a reduction in response size by about 70% (source).

If you are using WordPress as your back-end, just add this piece of code in your theme’s “functions.php”:

add_action('init','enable_gzip');
function enable_gzip() {
 ob_start('ob_gzhandler');
}

In case you’re not well familiar with editing WordPress theme files, there’s a neat plugin by James Socol which does exactly this. Download the plugin from here: Link

Concerned whether all browsers would support the Gzip function? Not to worry, most modern browsers support compressed pages. And if they don’t, then this function is simply returned false.