- /**
- * @author Seraphim
- * @copyright 2012
- * @link http://bbs.it-home.org
- */
- //
- function Sendheader ($last _modified, $p _type, $content _length = 0)
- {
- Set the client cache validity time
- Header ("Expires:".) Gmdate ("D, D M Y h:i:s", Time () + 15360000). "GMT");
- Header ("cache-control:max-age=315360000");
- Header ("Pragma:");
- Set Last Modified Time
- Header ("last-modified:".) $last _modified);
- Set File type information
- Header ($p _type);
- Header ("Content-length:".) $content _length);
- }
- Define (' Abspath ', DirName (__file__). '/');
- $cache = true;
- $cachedir = ' cache/'; directory where GZ files are stored, ensuring writable
- if (Empty ($_server[' query_string '))
- Exit ();
- $gzip = strstr ($_server[' http_accept_encoding '), ' gzip ');
- if (empty ($gzip))
- $cache = false;
- $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 '; Generate GZ file path
- $ext = Array_pop (Explode ('. ', $filename)); Determine file type information by suffix
- $type = "content-type:text/html"; The default file type
- Switch ($ext)
- {
- Case ' CSS ':
- $type = "Content-type:text/css";
- Break
- Case ' JS ':
- $type = "Content-type:text/javascript";
- Break
- Case ' gif ':
- $cache = false;
- $type = "Content-type:image/gif";
- Break
- Case ' jpg ':
- $cache = false;
- $type = "Content-type:image/jpeg";
- Break
- Case ' PNG ':
- $cache = false;
- $type = "Content-type:image/png";
- 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))
- {
- Same as the file modification date in the browser cache, returns 304
- Header ("http/1.1 304 not Modified");
- Send Client Header
- Header ("Content-encoding:gzip");
- Sendheader ($gmt _mtime, $type);
- }
- Else
- {
- Read GZ file output
- $content = file_get_contents ($cache _filename);
- Send Client Header
- Sendheader ($gmt _mtime, $type, strlen ($content));
- Header ("Content-encoding:gzip");
- Send data
- 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 ';
- Read file
- $content = file_get_contents ($filename);
- Remove the blank part
- $content = LTrim ($content);
- Compress file Contents
- $content = Gzencode ($content, 9, $gzip? Force_gzip:force_deflate);
- Send Client Header
- Sendheader ($gmt _mtime, $type, strlen ($content));
- Header ("Content-encoding:gzip");
- Send data
- Echo $content;
- Write file
- File_put_contents ($cache _filename, $content);
- }
- Else
- {
- Header ("http/1.0 404 Not Found");
- }
- }
- Else
- {//processing does not use the output in gzip mode. The principle of basic ibid.
- 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))
- {
- Same as the file modification date in the browser cache, returns 304
- Header ("http/1.1 304 not Modified");
- Send Client Header
- Sendheader ($gmt _mtime, $type, strlen ($content));
- Header ("Content-encoding:gzip");
- }
- Else
- {
- Read file output
- $content = file_get_contents ($filename);
- Send Client Header
- Sendheader ($gmt _mtime, $type, strlen ($content));
- Send data
- Echo $content;
- }
- }
- Else
- {
- Header ("http/1.0 404 Not Found");
- }
- }
- ?>
Copy CodeAttached--------------Instructions: 1, the server caches the compressed files, re-access to reduce the re-compression time, reduce CPU utilization. 2, by setting the client file cache time, reduce the number of re-requests, can be reduced by more than 85%. 3, image because it is already compressed format, just set the client cache time, do not do compression processing. Usage: 1, the server must support the Gzip,rewrite feature. 2, add the following code to the following line in the "rewritebase/" of the. htacess file: Rewriterule (. *.css$|. *.js$|. *.jpg$|. *.gif$|. *.png$) gzip.php?$1 [L] 3, upload gzip.php to root directory 4, the root directory to build the cache folder, guaranteed to read and write. |