Website development and design blog, tutorials and inspiration

Improve Website Speed by Enabling Compression

By Tim Trott, 22nd December 2013 in PHP

Google has long stated website speed will impact search ranking, but how do you go about improving your website speed?
 
Website Performance
  1. Why Website Loading Times Are Vital - How to Improve Yours
  2. Improve Website Speed by Enabling Compression
  3. Timing PHP Script Execution

There are two ways that you can improve your website speed by enabling compression for your generated content.

The first method works for static content, such as HTML and CSS, while the second only works on pages served generated with PHP.

htaccess GZip Compression

The simplest method for enabling GZip compression uses the Apache module mod_gzip which is available on most hosting platforms. Mod_gzip allows you to use the Gzip compression method to create a significant reduction in the volume of web page content served over the HTTP protocol.

If mod_gzip has not been installed on your server, and you have access to install modules, you can use this tutorial to install mod_gzip.

Once installed, enabling GZip compression is as easy as adding a few lines to the .htaccess file.

  1. <ifModule mod_gzip.c>
  2. mod_gzip_on Yes
  3. mod_gzip_dechunk Yes
  4. mod_gzip_item_include file .(html?|txt|css|js|php|pl)$
  5. mod_gzip_item_include handler ^cgi-script$
  6. mod_gzip_item_include mime ^text/.*
  7. mod_gzip_item_include mime ^application/x-javascript.*
  8. mod_gzip_item_exclude mime ^image/.*
  9. mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
  10. </ifModule>

If mod_gzip is not available, you can use the alternative mod_deflate which generally comes along with the apache package.

To enable mod_deflate simple add these lines to your .htaccess file.

  1. <ifmodule mod_deflate.c>
  2. AddOutputFilterByType DEFLATE text/text text/html text/plain text/xml text/css application/x-javascript application/javascript
  3. </ifmodule>

GZip Compression with PHP

PHP can easily compress generated content before sending it to the browser and works in the same way as I described a few years ago when I wrote about compressing JavaScript and CSS files.

The preferred method for enabling compression in PHP is to set the zlib.output_compression variable in php.ini

Name Default Changeable Changelog
zlib.output_compression "0" PHP_INI_ALL Available since PHP 4.0.5.
zlib.output_compression_level "-1" PHP_INI_ALL Available since PHP 4.3.0.
zlib.output_handler "" PHP_INI_ALL Available since PHP 4.3.0.

If you cant update your php.ini configuration file, you could try using ini_set php function.

  1. <?php
  2. ini_set("zlib.output_compression", "On");
  3. ?>

Another option you can use is to use PHP's output buffering functions.

  1. <?php
  2. function print_gzipped_page()
  3. {
  4. global $HTTP_ACCEPT_ENCODING;
  5. if( headers_sent() )
  6. {
  7. $encoding = false;
  8. }
  9. else if( strpos($HTTP_ACCEPT_ENCODING, 'x-gzip') !== false )
  10. {
  11. $encoding = 'x-gzip';
  12. }
  13. else if( strpos($HTTP_ACCEPT_ENCODING,'gzip') !== false )
  14. {
  15. $encoding = 'gzip';
  16. }
  17. else
  18. {
  19. $encoding = false;
  20. }
  21.  
  22. if($encoding)
  23. {
  24. $contents = ob_get_contents();
  25. ob_end_clean();
  26. header('Content-Encoding: '.$encoding);
  27. print("x1fx8bx08x00x00x00x00x00");
  28. $size = strlen($contents);
  29. $contents = gzcompress($contents, 9);
  30. $contents = substr($contents, 0, $size);
  31. print($contents);
  32. exit();
  33. }
  34. else
  35. {
  36. ob_end_flush();
  37. exit();
  38. }
  39. }
  40. ob_start();
  41. ob_implicit_flush(0);
  42.  
  43. // Do your PHP awesomeness here
  44. echo 'Hello World';
  45.  
  46. // finally, call this function to send the compressed page
  47. print_gzipped_page();
  48. ?>

Using either one of these methods should result in faster website download speed, but you still need to have an optimised high performance website to begin with. If your page takes 10 seconds to generate, all the GZipping in the world isn't going to improve site performance.

Once implemented, you should test your GZip compression to ensure that it working correctly. There are numerous sites that can do this for you.

Comments

There are no comments for this post. Be the first!

Leave a Reply

Your email address will not be published.