Compress CSS Using PHP | GZIP CSS Using PHP

By | August 7, 2013

Compress CSS Using PHP

Hello after making the PHP Page GZIP still we need to speed up our website a bit more so we have to GZIP our CSS too so that it will load fast and help our html to load fast also. In this article we are going to compress our CSS using PHP and GZIP the CSS. Same as the PHP Page GZIP script this script is also very short and sweet. Hope you will like it. Dont forget to share it.

In this article we are using css as external css. We will write our css style and a php page where we will call it.

CSS GZIP Script

Put this script at the starting in your css.

<?php
ob_start ("ob_gzhandler");
header("Content-type: text/css; charset: UTF-8");
header("Cache-Control: must-revalidate");
$offset = 60 * 60 ;
$ExpStr = "Expires: " .
gmdate("D, d M Y H:i:s",
time() + $offset) . " GMT";
header($ExpStr);
?>

Now save your css with .php extension. Please check again that you have changed the extension of your style file to .php from .css.

After these lines start writing your style codes.

Now just call your css in your php page same as we use to do earlier. Again we have to check for the extension we have to use .php else it will not work.

Your can check the demo here i am using the gzip css and php page gzip in my one of website here is the link  tellmequotes.com .

Without the GZIP i was having 75/100 as pagespeed from google but now i am having 91/100 check out

http://developers.google.com/speed/pagespeed/insights/?url=tellmequotes.com

Use the above code and take your website at another level.

Thank You. Like it  Use it Share it.

~~~~~ Happy Coding ~~~~~

4 thoughts on “Compress CSS Using PHP | GZIP CSS Using PHP

  1. Uphar Srivastava

    Very nice tutorial with easy implementation.
    Nice work Vivek.
    I want to ask one thing that as css can we also gzip js used in a site. I have searched through net and found a result but it is bit complicated, It said to combine css and js in combine.php. Can you provide us this tutorial also? Thanks in advance.

    Reply

Leave a Reply

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