国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > CSS > 正文

php使用gzip壓縮傳輸js和css文件的方法

2020-03-24 18:55:05
字體:
供稿:網(wǎng)友
本文實例講述了php使用gzip壓縮傳輸js和css文件的方法。分享給大家供大家參考。具體如下:
* 完整調(diào)用示例: * 1、combine.php t=j&b=html' target='_blank'>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 * 注:多個文件名之間用,分隔;只有一個文件名最后不要有, * 用,分隔的多個文件會被壓縮進(jìn)一個文件,一次性傳給瀏覽器 $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ā)點是網(wǎng)站根目錄 //基目錄參數(shù)不是必須的,如果有基目錄那么這個基目錄就會附加在文件名之前 $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 // 如果沒有基路徑 // 那么轉(zhuǎn)換后的文件名就是/public/js/jquery.js //多個文件名之間用,分隔 $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);希望本文所述對大家的php程序設(shè)計有所幫助。PHP教程

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請第一時間聯(lián)系我們修改或刪除,多謝。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 会昌县| 买车| 青河县| 仲巴县| 卢龙县| 牟定县| 五河县| 准格尔旗| 清丰县| 桃园市| 突泉县| 安庆市| 平乡县| 清水县| 峨边| 宁明县| 邵阳市| 长葛市| 大丰市| 军事| 龙州县| 玉门市| 定南县| 平昌县| 手游| 五家渠市| 丘北县| 扎鲁特旗| 灌云县| 双牌县| 平远县| 岳阳市| 慈溪市| 浙江省| 宿州市| 鹿泉市| 上饶市| 徐闻县| 克山县| 金坛市| 永登县|