iis php 开启gzip_在php虚拟主机中启用gzip压缩的方法

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 = $gzip ? '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_SINCE'])) ==  $gmt_mtime)

){

// 若文件无变动,返回304

header ("HTTP/1.1 304 Not Modified");

header("Expires: ");

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("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 www.jbxue.com

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'])) ==  $gmt_mtime)

){

header ("HTTP/1.1 304 Not Modified");

header("Expires: ");

header("Cache-Control: ");

header("Pragma: ");

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");

}

}

?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值