php使用gzip压缩传输js和css文件的方法
程序员文章站
2023-12-04 23:06:10
本文实例讲述了php使用gzip压缩传输js和css文件的方法。分享给大家供大家参考。具体如下:
本文实例讲述了php使用gzip压缩传输js和css文件的方法。分享给大家供大家参考。具体如下:
<?php /** * 完整调用示例: * 1、combine.php?t=j&b=public&fs=jslib.jquery,function * * 该例子调用的是网站根目录下的public/jslib/jquery.js和public/function.js * * 2、combine.php?t=j&fs=jslib.jquery,function * * 该例子调用的是网站根目录下的jslib/jquery.js和function.js * * 3、combine.php?t=c&b=public.css&fs=common,index * * 该例子调用的是网站根目录下的public/css/common.css和public/css/index.css * * 4、combine.php?t=c&fs=css.common * 该例子调用的是网站根目录下的css/common.css * * 注:多个文件名之间用,分隔;只有一个文件名最后不要有, * 用,分隔的多个文件会被压缩进一个文件,一次性传给浏览器 **/ $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文件夹,出发点是网站根目录 //基目录参数不是必须的,如果有基目录那么这个基目录就会附加在文件名之前 $base =isset($_get['b'])?($doc_root_uri.str_replace('.','/',$_get['b'])):$doc_root_uri; //文件名列表,文件名不带后缀名.比如基目录是 //文件名的格式是 :基目录(如果有)+文件包名+文件名 //例如:类型是j, // 文件名public.js.jquery // 如果有基路径且为public, // 那么转换后的文件名就是/public/public/js/jquery.js // 如果没有基路径 // 那么转换后的文件名就是/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程序设计有所帮助。
下一篇: C#实现带百分比的进度条功能示例