Define (' Abspath ', DirName (__file__). ' /');
$cache = true;//gzip compression switch
- $cachedir = ' gzip_cache/';//directory where GZ files are stored, 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 ');
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 the file name extension
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)
- ){
- Returns 304 if the file is not changed
- 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)) {//does not have a 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 CodeNext, add the following rule in. htaccess (Apache mod_rewrite) or Httpd.ini (IIS isapi_rewrite):
- Rewriterule (. *.css$|. *.js$)/gzip.php\?$1 [L]
Copy CodeFinally, the test is performed. Visit each page of the site to see if there is a cache file generation in the Gzip_cache folder. You can also use Baidu Webmaster tools, you can see whether the Css/js page compression. |