jump to navigation

How to Enable gZIP Compression For Your Web-pages with PHP January 18, 2012

Posted by Tournas Dimitrios in PHP.

Web-pages nowadays  include a lot of  client-side scripting code (for example JQuery and it’s supported plugins )  that  affect the latency and probably influence search engine rankings for these  pages . Latency is a big fancy word that simply means the amount of time between when something was started and when you can see its effects . A search engine may look at a range of information , probably latency is one of them ,  to make decisions whether it will visit and index pages on the Web , how it might rank those pages in search results , and how it may classify those pages .

PHP again has a solution for the aforementioned latency factor  , the gZIP compression utility . It reduces the size of you  files ,  improves the performance of your website and highly decreases its loading time . For complex websites it is highly recommended to enable it . The only requirement is  that your server has the zLib extension enabled , just run a phpinfo()  to verify that it’s enabled .

Some applications have internal support to compress their pages . For example , in Joomla you can turn on the gZIP compression from Global Configuration > Server > gZIP Page Compression set to ON .  If you are not using a web application that has internal methods for enabling the compression, you can add the following line at the top of your php files :


if (substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip')) ob_start("ob_gzhandler");
 else ob_start();


Basically, the user makes a request for your website , the server compresses your page (this significantly reduces its size) and transfers it to the customer’s computer. On the visitor’s end the file is being decompressed and visualized . The time needed for file compression is much less than the time to transfer a big file over the Internet . Although some file-formats  like images (jpeg ) are already compressed and won’t gain from the compression , other files like Javascript-Libraries and plain text files will improve the latency factor .

The results can be measured , by FF’s Firebug plugin or Chrome’s developer console , select the network tab and reload the page . Even a gain of  0.3 second  on the latency factor / page may not sound a big deal but  the overall benefit of your server on a 20K/day visitors website could be said it’s worth  .




No comments yet — be the first.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s