Go Back   SitePoint Forums > Forum Index > Program Your Site > PHP
Newsletter FAQ Members List Calendar Mark Forums Read

New to SitePoint Forums? Register here for free!

SitePoint Sponsor
 
Reply
 
Thread Tools Display Modes
Old Sep 14, 2004, 14:35   #1
mapg
SitePoint Enthusiast
 
Join Date: Jul 2004
Location: Florida
Posts: 48
Question JS and CSS Compression with PHP

Is there any way to configure PHP to compress not only PHP output but Javascripts files (.js) and CSS files (.css) that are referred into the PHP script?

I have enabled "zlib.output_compression on" and "zlib.output_compression_level 3", but I afraid that doesn't affect to JS and CSS file formats.

Thank you very much in advance.

Mapg
mapg is offline   Reply With Quote
Old Sep 14, 2004, 16:14   #2
FreMEliCa
SitePoint Member
 
FreMEliCa's Avatar
 
Join Date: Jun 2003
Location: Canada
Posts: 14
Here:
http://fiftyfoureleven.com/sandbox/w...s-gzip-method/

Similar process for JS I think.
FreMEliCa is offline   Reply With Quote
Old Sep 14, 2004, 16:58   #3
mapg
SitePoint Enthusiast
 
Join Date: Jul 2004
Location: Florida
Posts: 48
I have added AddType application/x-httpd-php .php .css .js

and works ... At least I see these files (.css and .js) as gzipped at ...

http://leknor.com/code/gziped.php

So I am not sure if it's necessary to add some kind of header to css or js files like as follow ...

PHP Code:

<?php

ob_start
();
header("Content-Type: text/css");
?>
... or an prepend_file tag in .htaccess where styles are placed, namely ...

php_value auto_prepend_file gzip-css.php (as noted in the link that you provided)

Is it enough if I saw gzipped my css files at http://leknor.com/code/gziped.php?

Thanks

Mapg
mapg is offline   Reply With Quote
Old Sep 15, 2004, 04:29   #4
reminder
SitePoint Wizard
 
Join Date: Mar 2004
Posts: 1,647
congratulation for the site and the script!
really impressive!
nice job
reminder is offline   Reply With Quote
Old Sep 16, 2004, 17:25   #5
FreMEliCa
SitePoint Member
 
FreMEliCa's Avatar
 
Join Date: Jun 2003
Location: Canada
Posts: 14
From:
http://www.fiftyfoureleven.com/sandb...s-gzip-method/

1. You save this snippet as gzip-css.php:
Code:
<?php 
ob_start ("ob_gzhandler");
header("Content-type: text/css");
header("Cache-Control: must-revalidate");
$offset = 60 * 60 ;
$ExpStr = "Expires: " . 
gmdate("D, d M Y H:i:s",
time() + $offset) . " GMT";
header($ExpStr);
?>
2. Add this snippet to your htaccess:
Code:
AddHandler application/x-httpd-php .css
php_value auto_prepend_file gzip-css.php
Make sure that if you do this that your CSS files are in a directory all by themselves.

Since you probably don't want to do that, and you want your JS gzipped you should do the following:

Keep your htaccess rule of:
Code:
AddType application/x-httpd-php .php .css .js
Then put this at the start of your CSS files:
Code:
<?php 
ob_start ("ob_gzhandler");
header("Content-type: text/css");
header("Cache-Control: must-revalidate");
$offset = 60 * 60 ;
$ExpStr = "Expires: " . 
gmdate("D, d M Y H:i:s",
time() + $offset) . " GMT";
header($ExpStr);
?>
and this at the start of your JS files:
Code:
<?php 
ob_start ("ob_gzhandler");
header("Content-type: text/javascript");
header("Cache-Control: must-revalidate");
$offset = 60 * 60 ;
$ExpStr = "Expires: " . 
gmdate("D, d M Y H:i:s",
time() + $offset) . " GMT";
header($ExpStr);
?>
Remember that we need to make sure that our CSS files, though being parsed by PHP need to be sent with a MimeType of text/css or it won't display in stricter browsers, and similarly for JS.
FreMEliCa is offline   Reply With Quote
Old Sep 16, 2004, 20:40   #6
mapg
SitePoint Enthusiast
 
Join Date: Jul 2004
Location: Florida
Posts: 48
Thumbs up

Thank you very much for your reply.

Seems that configuring PHP with ...

Code:
zlib.output_compression on
or with ...

Code:
php_flag zlib.output_compression on
in the main .htaccess (root directory)

... it's not necessary to add "ob_gzhandler" in every css or js file.

After that,

Code:
AddType application/x-httpd-php .php .css .js
... is a must, of course.

I am agree with you that ...

Code:
header("Content-type: text/css");
and ...

Code:
header("Content-type: text/javascript");
... is a good option to avoid problems with strict browsers

But ...
Code:
* snip * 
header("Cache-Control: must-revalidate");
$offset = 60 * 60 ;
$ExpStr = "Expires: " . 
gmdate("D, d M Y H:i:s",
time() + $offset) . " GMT";
header($ExpStr);
* snip *
... could be handled by the html code itself with common tags.

I also added ...

Code:
zlib.output_compression_level 3
because works fine and CPU is not overloaded.

... or if you prefer a htaccess file ...

Code:
php_value zlib.output_compression_level 3
I suggest do several tests at ...
http://leknor.com/code/gziped.php

Best Regards,

Mapg
mapg is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread | Next Thread »

Thread Tools
Display Modes

 
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Sponsored Links
 
Forum Jump


All times are GMT -7. The time now is 07:29.


Powered by vBulletin® Version 3.7.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Copyright 1998-2009, SitePoint Pty Ltd. All Rights Reserved