PHP - sending gzip compressed JS / CSS

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 am doing this for a public application. which can be downloaded by anyone. Thus, on shared hosts there will be people with gzip disabled

+7
javascript css php gzip
source share
5 answers

No, not okay. There are a lot of things wrong. Enable, do not die after redirection, not considering the descent method, ...

This is very easy to do with PHP, since 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'); 
+10
source share

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

Add the following files to the .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 

(i.e. one per mime type)

+3
source share

The server should do this automatically if configured correctly.

+2
source share

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

 AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript text/javascript-x application/javascript 
+2
source share

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> 

We recommend creating a css folder and putting files there.

With RewriteRule you do not need to set header('Content-type: text/css'); and other functions install gzip on the server before the php process. The code is now faster!

0
source share

All Articles