php使用gzip壓縮傳輸js和css文件的方法
本文實(shí)例講述了php使用gzip壓縮傳輸js和css文件的方法。分享給大家供大家參考。具體如下:
<?php /** * 完整調(diào)用示例: * 1、combine.php?t=j&b=public&fs=jslib.jquery,function * * 該例子調(diào)用的是網(wǎng)站根目錄下的public/jslib/jquery.js和public/function.js * * 2、combine.php?t=j&fs=jslib.jquery,function * * 該例子調(diào)用的是網(wǎng)站根目錄下的jslib/jquery.js和function.js * * 3、combine.php?t=c&b=public.css&fs=common,index * * 該例子調(diào)用的是網(wǎng)站根目錄下的public/css/common.css和public/css/index.css * * 4、combine.php?t=c&fs=css.common * 該例子調(diào)用的是網(wǎng)站根目錄下的css/common.css * * 注:多個(gè)文件名之間用,分隔;只有一個(gè)文件名最后不要有, * 用,分隔的多個(gè)文件會(huì)被壓縮進(jìn)一個(gè)文件,一次性傳給瀏覽器 **/ $is_bad_request=false; $cache = true; $doc_root_uri=$_SERVER['DOCUMENT_ROOT'].'/'; $cachedir = $doc_root_uri . 'public/cache'; //文件類型,j為js,c為css $type=isset($_GET['t'])?($_GET['t']=='j'||$_GET['t']=='c'?$_GET['t']:''):''; //存放js和css文件的基目錄, 例如:?b=public.js 代表的是/public/js文件夾,出發(fā)點(diǎn)是網(wǎng)站根目錄 //基目錄參數(shù)不是必須的,如果有基目錄那么這個(gè)基目錄就會(huì)附加在文件名之前 $base =isset($_GET['b'])?($doc_root_uri.str_replace('.','/',$_GET['b'])):$doc_root_uri; //文件名列表,文件名不帶后綴名.比如基目錄是 //文件名的格式是 :基目錄(如果有)+文件包名+文件名 //例如:類型是j, // 文件名public.js.jquery // 如果有基路徑且為public, // 那么轉(zhuǎn)換后的文件名就是/public/public/js/jquery.js // 如果沒(méi)有基路徑 // 那么轉(zhuǎn)換后的文件名就是/public/js/jquery.js //多個(gè)文件名之間用,分隔 $fs=isset($_GET['fs'])?str_replace('.','/',$_GET['fs']):''; $fs=str_replace(',','.'.($type=='j'?'js,':'css,'),$fs); $fs=$fs.($type=='j'?'.js':'.css'); if($type==''||$fs==''){$is_bad_request=true;} //die($base); if($is_bad_request){header ("HTTP/1.0 503 Not Implemented");} $file_type=$type=='j'?'javascript':'css'; $elements = explode(',',preg_replace('/([^?]*).*/', '\1', $fs)); // Determine last modification date of the files $lastmodified = 0; while (list(,$element) = each($elements)) { $path =$base . '/' . $element; if (($type == 'j' && substr($path, -3) != '.js') || ($type == 'c' && substr($path, -4) != '.css')) { header ("HTTP/1.0 403 Forbidden"); exit; } if (substr($path, 0, strlen($base)) != $base || !file_exists($path)) { header ("HTTP/1.0 404 Not Found"); exit; } $lastmodified = max($lastmodified, filemtime($path)); } // Send Etag hash $hash = $lastmodified . '-' . md5($fs); header ("Etag: \"" . $hash . "\""); if (isset($_SERVER['HTTP_IF_NONE_MATCH']) && stripslashes($_SERVER['HTTP_IF_NONE_MATCH']) == '"' . $hash . '"') { // Return visit and no modifications, so do not send anything header ("HTTP/1.0 304 Not Modified"); header ("Content-Type: text/" . $file_type); header ('Content-Length: 0'); } else { // First time visit or files were modified if ($cache) { // Determine supported compression method $gzip = strstr($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip'); $deflate = strstr($_SERVER['HTTP_ACCEPT_ENCODING'], 'deflate'); // Determine used compression method $encoding = $gzip ? 'gzip' : ($deflate ? 'deflate' : 'none'); // Check for buggy versions of Internet Explorer if (!strstr($_SERVER['HTTP_USER_AGENT'], 'Opera') && preg_match('/^Mozilla\/4\.0 \(compatible; MSIE ([0-9]\.[0-9])/i', $_SERVER['HTTP_USER_AGENT'], $matches)) { $version = floatval($matches[1]); if ($version < 6) $encoding = 'none'; if ($version == 6 && !strstr($_SERVER['HTTP_USER_AGENT'], 'EV1')) $encoding = 'none'; } // Try the cache first to see if the combined files were already generated $cachefile = 'cache-' . $hash . '.' . $file_type . ($encoding != 'none' ? '.' . $encoding : ''); if (file_exists($cachedir . '/' . $cachefile)) { if ($fp = fopen($cachedir . '/' . $cachefile, 'rb')) { if ($encoding != 'none') { header ("Content-Encoding: " . $encoding); } header ("Content-Type: text/" . $file_type); header ("Content-Length: " . filesize($cachedir . '/' . $cachefile)); fpassthru($fp); fclose($fp); exit; } } } // Get contents of the files $contents = ''; reset($elements); while (list(,$element) = each($elements)) { $path = $base . '/' . $element; $contents .= "\n\n" . file_get_contents($path); } // Send Content-Type header ("Content-Type: text/" . $file_type); if (isset($encoding) && $encoding != 'none') { // Send compressed contents $contents = gzencode($contents, 9, $gzip ? FORCE_GZIP : FORCE_DEFLATE); header ("Content-Encoding: " . $encoding); header ('Content-Length: ' . strlen($contents)); echo $contents; } else { // Send regular contents header ('Content-Length: ' . strlen($contents)); echo $contents; } // Store cache if ($cache) { if ($fp = fopen($cachedir . '/' . $cachefile, 'wb')) { fwrite($fp, $contents); fclose($fp); } } }
希望本文所述對(duì)大家的php程序設(shè)計(jì)有所幫助。
相關(guān)文章
PHP Ajax實(shí)現(xiàn)無(wú)刷新附件上傳
這篇文章為大家詳細(xì)主要介紹了PHP Ajax實(shí)現(xiàn)無(wú)刷新附件上傳功能的具體代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-08-08PHP保存Base64圖片base64_decode的問(wèn)題整理
在本篇文章里小編給大家整理的是關(guān)于PHP保存Base64圖片base64_decode的問(wèn)題,需要的朋友們參考下。2019-11-11php中cookie與session的區(qū)別點(diǎn)總結(jié)
在本篇文章里小編給大家整理的是一篇關(guān)于php中cookie與session的區(qū)別點(diǎn)總結(jié)內(nèi)容,有興趣的朋友們可以參考學(xué)習(xí)下。2021-12-12php正則preg_replace_callback函數(shù)用法實(shí)例
這篇文章主要介紹了php正則preg_replace_callback函數(shù)用法,實(shí)例分析了preg_replace_callback函數(shù)進(jìn)行正則替換的相關(guān)技巧,需要的朋友可以參考下2015-06-06不支持fsockopen但支持culr環(huán)境下下ucenter與modoer通訊問(wèn)題
網(wǎng)站上線,modoer與ucenter 下不能通訊折騰了我差不多二天,開(kāi)始都以為自己的配置出問(wèn)題,移植了平臺(tái)后就不能通訊了,修改了幾次配置,都沒(méi)有成功2011-08-08php實(shí)現(xiàn)求相對(duì)時(shí)間函數(shù)
這篇文章主要介紹了php實(shí)現(xiàn)求相對(duì)時(shí)間函數(shù),可實(shí)現(xiàn)簡(jiǎn)單求相對(duì)時(shí)間為幾分鐘前或幾小時(shí)前的功能,非常簡(jiǎn)單實(shí)用,需要的朋友可以參考下2015-06-06