-
-
define('ABSPATH', dirname(__FILE__).'/');
-
$cache = true;//Gzip壓縮開關
- $cachedir = 'gzip_cache/';//存放gz檔案的目錄,使用前創建,並賦予可寫入權限
$gzip = strstr($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip');
- $deflate = strstr($_SERVER['HTTP_ACCEPT_ENCODING'], 'deflate');
- $encoding = $gzipicoding ' 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';//快取路徑
- $type="Content-type: text /html"; //MIME資訊
- $ext = array_pop(explode('.', $filename));//取得檔案副檔名
switch ($ext){/ /更新MIME資訊
- 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)){//假如存在gz檔
-
- $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_SINCg')dFIED_SINCg_mtVERm 🎜> ){
-
- // 若檔案無變動,回傳304
- header ("HTTP/1.1 304 Not Modified");
- header("Expires: ");
- header(> header(> header(> header(> header(> header(> header(> header(>) "Cache-Control: ");
- header("Pragma: ");
- header($type);
- header("Tips: Cache Not Modified (Gzip)");
- header ( 'Content-Length: 0');
}else{
//讀取gz檔輸出
- $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)){ //沒有對應的gz檔
$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);//壓縮內容
-
- header("Last-Modified:" . $gmt_mtime);
- header("Expires: ");
- header("Expires: ");
- header("Expires: ");
- header("Expires: ");
- 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')) {//寫入快取
- fwrite($fp, $content);
- fclose($fp);
- }
- }else{
- header("HTTP/1.0 404 Not Found");
- }
- }else{ //關閉Gzip壓縮
- //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'])) ===g; 🎜>
- header ("HTTP/1.1 304 Not Modified");
- header("Expires: ");
- header("Cache-Control: ");
- header("Pmarag: " );
- 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");
- }
- }
- ?>
-
複製程式碼接著,在.htaccess(Apache mod_rewrite)或httpd.ini(IIS ISAPI_Rewrite)中加入以下規則:
-
- RewriteRule (.*.css$|.*.js$) /gzip.php?$1 [L]
複製程式碼
最後,進行測試。
造訪網站的各個頁面,看看gzip_cache資料夾中是否有快取檔案產生。
也可以用百度站長工具,可以看到css/js頁面是否壓縮。 |