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

首頁 > 網站 > 建站經驗 > 正文

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

2024-04-25 20:40:15
字體:
來源:轉載
供稿:網友

本文實例講述了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程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 稻城县| 镇雄县| 始兴县| 嘉鱼县| 安徽省| 平远县| 靖远县| 登封市| 襄垣县| 彰武县| 将乐县| 仙游县| 诏安县| 金昌市| 仪陇县| 平果县| 乐亭县| 类乌齐县| 华池县| 白银市| 阳新县| 丹阳市| 都兰县| 冷水江市| 宁阳县| 乐都县| 托克逊县| 黎平县| 宁武县| 繁峙县| 马山县| 霍城县| 兴义市| 加查县| 汽车| 吉安市| 济南市| 岳阳县| 元阳县| 赤峰市| 石楼县|