Papermashup

Subscribe


Tweets


"RT @charlesLeahy1: Bloody migrants coming over here blocking our roads #amphibiancaravan https://t.co/smmvml8NTA"

@ashleyford 13 hours ago

"RT @RandyRainbow: This is the only news I care about today 🦃 https://t.co/iWRg3aNujp"

@ashleyford 4 days ago

Designer and web developer, Co-founder and Technical Director at Harkable.com. Previously I worked at Spotify, MySpace and InMobi. Contact me - ashley[at]papermashup.com

Use PHP to Gzip CSS files

AshleyAshley

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();
  }

Designer and web developer, Co-founder and Technical Director at Harkable.com. Previously I worked at Spotify, MySpace and InMobi. Contact me - ashley[at]papermashup.com


Warning: count(): Parameter must be an array or an object that implements Countable in /nfs/c02/h08/mnt/40992/domains/papermashup.com/html/wp-includes/class-wp-comment-query.php on line 399
Comments 3
  • Arthur
    Posted on

    Arthur Arthur

    Reply Author

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