Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I would like to copress my JavaScript and CSS files and I use a PHP file with the following code:

// compression.php
ob_start ( 'ob_gzhandler' );

$content = file_get_contents ( $_GET [ 'path' ] );
$extension = strtolower ( substr ( strrchr ( $_GET [ 'path' ], "." ), 1 ) );
header ( "Content-Type:" . ( $extension == 'css' ? 'text/css' : 'text/javascript' ) );
header ( "Cache-Control: no-cache, must-revalidate" );
header ( "Expires: Sat, 26 Jul 1997 05:00:00 GMT" );
echo $content;

HTML:

<link rel="stylesheet" type="text/css" href="compression.php?path=../assets/css/jquery-ui.css" />

<script type="text/javascript" src="compression.php?path=../assets/js/jquery.min.js">

Is this good way or not?

share|improve this question
1  
"...this question will likely solicit opinion, debate, arguments, polling, or extended discussion." If you need a code review, there is a site for that. – George Cummins Jul 18 '11 at 18:35
when no answer was helpful why should i accept any answer!???? – Hamid Seyyedi Jul 18 '11 at 18:41
If no answer was helpful, you may be asking the wrong questions. – George Cummins Jul 18 '11 at 18:43
it's funny man!maybe no one can't help me about it!you mean all answer in this site is helpful ? – Hamid Seyyedi Jul 18 '11 at 18:47
You know, you may be right. Maybe no one can help you. – George Cummins Jul 18 '11 at 18:54

2 Answers

up vote 5 down vote accepted

No it definitely is not.

What about compression.php?path=compression.php? One could view all your PHP and server files which is an severe security issue.

What's more, the server will spend a lot of its power to compressing at each page request.

Why not just minify your code and save it statically instead. It will be much more effective.

share|improve this answer
+1 - kudos for pointing out security flaw – mrtsherman Jul 18 '11 at 18:37

If you want to compress your JS:

Use packer, with Shrink variables checked.

To compress the CSS:

Use Clean CSS, with Sort Properties, and Remove last ; checked.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.