7

I created a style.css.php file with this code:

<?php

  $gzip = (ob_get_length() === false && !ini_get("zlib.output_compression") && ini_get("output_handler") != "ob_gzhandler" && extension_loaded("zlib") && substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') && !headers_sent());

  if(!$gzip) header('Location: style.css');

  header('Content-type: text/css');
  header('Cache-Control: no-cache');
  header('Expires: Mon, 1 Jan 1901 04:20:00 GMT');

  ob_start('ob_gzhandler');

  include "style.css";
?>

What do you think? Is this a good way to compress js/css files? Is there a better way to do this? I'm doing this for a public app. that can be downloaded by anyone. So there will be people on shared hosts with gzip disabled

5
  • I assume your server doesn't support compression? Commented Nov 15, 2010 at 22:23
  • What you try to achieve by this code ? Expected Advantage ? Commented Nov 15, 2010 at 22:24
  • well I'm testing this on a shared server, and compression works. Locally works too. Or do you mean automatic compression done by the server? Is that possible? Commented Nov 15, 2010 at 22:25
  • 4
    Rule #1 of PHP: Giant single lines of code help nobody. Commented Nov 16, 2010 at 0:54
  • Its better to use pre-compressed js/css file instead of compressing it with php "on the fly" Commented Sep 18, 2014 at 7:50

5 Answers 5

12

No, not OK. There's a lot of things wrong there. The include, no dying after redirecting, not considering the deflate method, ...

This is very simple to do with PHP, as the zlib output handler automatically detects the appropriate compression to send to the client (if any); all you have to do is enable it:

<?php
if (extension_loaded("zlib") && (ini_get("output_handler") != "ob_gzhandler")) {
    ini_set("zlib.output_compression", 1);
}

readfile('style.css');
Sign up to request clarification or add additional context in comments.

3 Comments

thank you :) may I ask why readfile() is better than include? and should I still redirect if the condition is false? I used header("Location") because I thought it would save some server resources.
& if I try to enable output compression in generated html, I get a notice: Notice: ob_end_flush() [ref.outcontrol]: failed to delete buffer zlib output compression :(
readfile() just sucks up the file in chunks and sends them out. include/require will run the data through the PHP parser first, which is a useless step if the file's pure CSS.
3

If you're serving your site using Apache, you can use either mod_gzip or mod_deflate. They are usually available on shared hosts and can be configured in .htaccess files.

Add the following lines to your .htaccess file:

AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript

(ie one per mime-type)

1 Comment

It does not need to be one per line. See my answer: stackoverflow.com/questions/4189429/…
2

Server should do that automagically if configured properly.

Comments

2

Adam is on the right track but it does not need to be one MIME type per line. See the Apache2 manual for more info on the AddOutputFilterByType directive.

AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript text/javascript-x application/javascript

Comments

0

First SET in '.htaccess'

RewriteEngine on
RewriteRule style.css style.css.php
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript
SetOutputFilter DEFLATE

BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4.0[678] no-gzip
BrowserMatch MSI[E] !no-gzip !gzip-only-text/html

SetEnvIfNoCase Request_URI 
.(?:gif|jpe?g|png)$ no-gzip dont-vary
</IfModule>

Recomended create css folder and put files there.

With RewriteRule you don't need set header('Content-type: text/css'); and other functions set gzip on server before php process. the code running more fast now!

1 Comment

Important! Some browsers don't suport gzip for css, then set gzip only text/html. More recomended use .htaccess with this rules.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.