/ Published in: PHP
The code is not particularly pretty but it does the job.
If you call this file css.php you can call it like this:
css.php?files=reset,style,iefix&minify=yes
If you call this file css.php you can call it like this:
css.php?files=reset,style,iefix&minify=yes
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
// css.php /* If you name this file "css.php" then you can call it using a GET query string like this: - css.php?files=reset,style,iefix&minify=yes There are two parts to the query string: 1. The "files" part. Add your file names (with or without .css) in a comma-separated list. 2. The "minify" part. You can add "yes" to it, or omit it entirely. When you have done this and loaded the file once the script will cache, update, serve files and handle everything else. You only have to specify which files should be loaded and if it should be minified. The script will automatically update the cached files whenever they become outdated, so you can edit your individual CSS files as much as you like without worrying about getting the most recent file. */ // Handle the query string } else { $files = null; } } else { $minify = false; } // Get the file names $filesMissing = 0; foreach ($files as $key => &$name) { continue; } $name .= '.css'; } $filesMissing++; } } // If there are no file names, or if a file does not exist // then 404; Houston we have a problem... } // Is there a cached version and is it newer than the non-cached // files? If there is one then go directly to output // !!! Make sure the directory "cached" is writeable !!! $useCache = false; foreach ($dates as $d) { if ($cachedLastModified < $d) { $useCache = false; break; } } if ($useCache) { goto output; } } // If there is no cached version, or if the cached version is older // than the css files, then get the code from the css files and save a // cached version for future use $code = ''; foreach ($files as $name) { } if ($minify) { $code = minifyCSS($code); } // The output output: { $encoding = false; $encoding = 'x-gzip'; } $encoding = 'gzip'; } if ($encoding) { } echo $code; } // Function for minifying CSS code // Removes or truncates unecessary characters, whitespace and newlines function minifyCSS($code) { }