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开发者_StackOverflow" 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?
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.
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.
精彩评论