-
-
define('ABSPATH', dirname(__FILE__).'/');
$cache = true;//Gzip compression Switch
- $cachedir = 'gzip_cache/';//The directory where gz files are stored is created before use and given writable permissions
$gzip = strstr($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip');
- $deflate = strstr($_SERVER['HTTP_ACCEPT_ENCODING'], 'deflate');
- $encoding = $gzip ? 'gzip' : ($deflate ? 'deflate' : 'none');< /p>
if(!isset($_SERVER['QUERY_STRING'])) exit();
$key=array_shift(explode('?', $_SERVER[ 'QUERY_STRING']));
- $key=str_replace('../','',$key);
- $filename=ABSPATH.$key;
- $symbol='^';
- $rel_path=str_replace(ABSPATH ,'',dirname($filename));
- $namespace=str_replace('/',$symbol,$rel_path);
- $cache_filename=ABSPATH.$cachedir.$namespace.$symbol.basename($filename).' .gz';//Cache path
- $type="Content-type: text/html"; //MIME information
- $ext = array_pop(explode('.', $filename));//Get file extension< ;/p>
switch ($ext){//Update MIME information
- case 'css':
- $type="Content-type: text/css";
- break;
- case 'js':
- $type="Content-type: text/javascript";
- break;
- default:
- exit();
- }
if($cache){
- if(file_exists($cache_filename) ){//If there is a gz file
-
- $mtime = filemtime($cache_filename);
- $gmt_mtime = gmdate('D, d M Y H:i:s', $mtime) . 'GMT';
-
- if( (isset ($_SERVER['HTTP_IF_MODIFIED_SINCE']) &&
- array_shift(explode(';', $_SERVER['HTTP_IF_MODIFIED_SINCE'])) == $gmt_mtime)
- ){
-
- // If the file has not changed, return 304
- header ( "HTTP/1.1 304 Not Modified");
- header("Expires: ");
- header("Cache-Control: ");
- header("Pragma: ");
- header($type);
- header(" Tips: Cache Not Modified (Gzip)");
- header ('Content-Length: 0');
}else{
//Read gz file output
- $content = file_get_contents($cache_filename);
- header("Last-Modified:" . $gmt_mtime);
- header("Expires: ");
- header("Cache-Control: ");
- header( "Pragma: ");
- header($type);
- header("Tips: Normal Respond (Gzip)");
- header("Content-Encoding: gzip");
- echo $content;
- }
}else if(file_exists($filename)){ //There is no corresponding gz file
$mtime = mktime();
- $gmt_mtime = gmdate('D, d M Y H:i:s', $mtime) . ' GMT';
-
- $content = file_get_contents($filename);
- $content = gzencode($content, 9, $gzip ? FORCE_GZIP : FORCE_DEFLATE);//Compressed content
-
- header("Last-Modified:" . $gmt_mtime);
- header("Expires: ");
- header("Cache-Control: ");
- header("Pragma: ");
- header($type) ;
- header("Tips: Build Gzip File (Gzip)");
- header ("Content-Encoding: " . $encoding);
- header ('Content-Length: ' . strlen($content));
- echo $ content;
if ($fp = fopen($cache_filename, 'w')) {//Write cache
- fwrite($fp, $content);
- fclose($fp);
- }
}else{
- header("HTTP/1.0 404 Not Found");
- }
- }else{ //Turn off Gzip compression
- //by bbs.it-home.org
- if(file_exists($filename)){
- $mtime = filemtime($filename);
- $gmt_mtime = gmdate('D, d M Y H:i:s', $mtime) . ' GMT';
-
- if( ( isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) &&
- array_shift(explode(';', $_SERVER['HTTP_IF_MODIFIED_SINCE'])) == $gmt_mtime)
- ){
-
- header ("HTTP/1.1 304 Not Modified") ;
- header("Expires: ");
- header("Cache-Control: ");
- header("Pragma: ");
- header($type);
- header("Tips: Cache Not Modified");
- header ('Content-Length: 0');
-
- }else{
-
- header("Last-Modified:" . $gmt_mtime);
- header("Expires: ");
- header("Cache-Control: ") ;
- header("Pragma: ");
- header($type);
- header("Tips: Normal Respond");
- $content = readfile($filename);
- echo $content;
-
- }
- }else{
- header("HTTP/1.0 404 Not Found");
- }
- }
- ?>
-
-
Copy code
Next, add the following rules in .htaccess (Apache mod_rewrite) or httpd.ini (IIS ISAPI_Rewrite):
-
- RewriteRule (.*.css$|.*.js$) /gzip.php?$1 [L]
Copy the code
Finally, test it.
Visit each page of the website to see if there are cache files generated in the gzip_cache folder.
You can also use Baidu Webmaster Tools to see whether the css/js page is compressed.
|