I've already wrote about "
firefox only includes styles form .css files" and I found out that isn't quite true. Actually firefox doesn't care about the css file extension, but only relay on header
Content-Type: text/css. If you as usual have .css file, apache will take care of right content-type, otherwise you will have to take care about it.
header('Content-Type: text/css');I came across this when i was writing this script:
## combine.php
// Define path prefix
$prefix = realpath('.');
if($_GET['js']) {
header('Content-Type: application/x-javascript');
$data = Array('path'=>$prefix.'/template/js/','files'=>$_GET['js'],'ext'=>'.js');
} elseif($_GET['css']) {
header('Content-Type: text/css');
$data = Array('path'=>$prefix.'/template/styles/','files'=>$_GET['css'],'ext'=>'.css');
} else exit();
foreach(explode(',',$data['files']) as $file) {
if($file && file_exists($data['path'].$file.$data['ext'])) readfile($data['path'].$file.$data['ext']);
}Simple few lines of code, but already do the trick. That will minimise
number of server requests and benefit in faster page loading.
Defining the path and file extension is important, otherwise you may expose some other scripts source or even worse, some system files.
Usage in page head<link rel="stylesheet" type="text/css" href="combine.php?css=style,forms" />
<script type="text/javascript" src="combine.php?js=mootools,validate,fx"></script>
Hope you like it, otherwise If you want more advanced solution try
minify.
cheers
Have you ever want to prevent others sticking their fingers into your code? I did. I have searched over the web, but i didn't find anything suitable. Actually i didn't want to protect my code, but just make it harder to read and modify. So i made small script that do just that. The script strips all new lines, tabs, unneeded spaces and comments, so the code is braked in to one line. I didn't forget about php "here document" that i use quite a lot. This one needs new line right after the terminator, so if you using it, the output will have some more lines on the end.
I run this script from the linux shell as you see below, stored in to file comp.php. Just "cd" to directory where you have your php or js scripts and run the command. Find will recursively found all the *.php scripts and obfuscate them.
Be careful before running the script!!! Always check the path you are in and do a backup copy, otherwise you might end up with a headache or even losing the code.
Usage
find . -iname "*.php" -exec php comp.php {} \;
find . -iname "*.js" -exec php comp.php {} \;
script:
// here document terminator
$here_document = "END";
error_reporting(0);
if(ereg($argv[0],$argv[1])) exit();
function cleanup($line){
$line = preg_replace('/(^|\s)\/\/(.*)\n/','',$line); // remove line comments // comment
$line = preg_replace('/(\t|\r|\n)/','',$line); // remove new lines \n, tabs and \r
return $line;
}
$lines = file($argv[1]);
echo $argv[1]." ";
echo "lines: ".count($lines)." -> ";
foreach ($lines as $line) {
if(ereg($here_document,$line) && ereg("<<<",$line)) $out.= cleanup($line)."\n";
else if(ereg("$here_document;",$line)) $out.= "\n".cleanup($line)."\n";
else $out.= trim(cleanup($line))." ";
}
$out = preg_replace('/(\/\*[\s\S]*?\*\/)/', '', $out); // remove multi-line comments /* */
file_put_contents($argv[1],trim($out)); // store back to file
echo count(file($argv[1]))."\n";
Source:
comp.php.txt