Use PHP to Gzip CSS files

Use PHP to Gzip CSS files

1 Star2 Stars3 Stars4 Stars5 Stars
Posted on May 18, 2009

Minimising the time a user has to wait for a web page to load is crucial. Using Gzip and PHP we can minimise file sizes of CSS files. This technique is a powerful and simple way to reduce page download size and speed up your site that will work with most PHP installations, even those on shared hosting that don’t have mod_deflate turned on in their Apache configuration.

Just add the code below at the top of your page. You then need to change the links to point to your CSS files.


  if(extension_loaded('zlib')){
    ob_start('ob_gzhandler');
  }
  header ("content-type: text/css; charset: UTF-8");
  header ("cache-control: must-revalidate");
  $offset = 60 * 60;
  $expire = "expires: " . gmdate ("D, d M Y H:i:s", time() + $offset) . " GMT";
  header ($expire);
  ob_start("compress");
  function compress($buffer) {
      // remove comments
      $buffer = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $buffer);
      return $buffer;
    }

   // list CSS files or JS to be included in the Gzip
    include('global.css');
    include('style.css');

  if(extension_loaded('zlib')){
    ob_end_flush();
  }

Recent shares

More tutorials from Papermashup
Comments
3 discussions around Use PHP to Gzip CSS files
  1. Pingback: Enable Gzip in .htaccess with mod_deflate | Sevenpixels Web Design

  2. Pingback: 25 New & Useful PHP Techniques & Tutorials

  3. Arthur says:

    Did you by any chance test this script for caching??? It’s not working on text/css files

Leave a Reply

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

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Looking for a registry cleaner to speed up your PC and show a full diagnostics?
Faster surfing with Dish Network High Speed Internet

Never miss an update from Papermashup

Get notified about the latest tutorials and downloads.

Subscribe by Email

Get alerts directly into your inbox after each post and stay updated.
Subscribe
OR

Subscribe by RSS

Add our RSS to your feedreader to get regular updates from us.
Subscribe

Get in contact

Please use the form below to get in touch.

About Me

I'm Ashley Ford, Co-founder and Technical Director at Harkable.com London, UK. Previously I worked at InMobi, Spotify and MySpace. My interests include photography and making short videos I'm also an avid F1 fan. I'm always working on side projects. Here are a few: Easy Poll, We Deliver.



What do you specialise in?

I spend a lot of time coding in PHP and MySQL, as well as front end XHTML and CSS. I also specialise in javascript and the jQuery framework as well as being an avid designer. You can find me on dribbble

Interested in advertising?

If you'd like to advertise on Papermashup.com you can find details here Or use the contact link below for further advertising opportunities.

How do I contact you

You can contact me here. and I'm available for consultation, freelance, programming book reviews.

Get on the mailing list

Join over 3000 people who have subscribed to the Papermashup inbox message, and be the first to find out about tutorial, competitions and giveaways.