1,在服务器缓存了压缩过的文件,再次访问减少再压缩时间,降低cpu占用率。
2,通过设置客户端文件缓存时间,降低再次请求次数,可降低85%以上。
3,图片因为已经是压缩格式,只是设置客户端缓存时间,不做压缩处理。
使用方法:
1,服务器必须支持gzip,rewrite功能。
2,在.htacess文件的“rewritebase /”下面一行添加下面的代码,见图
rewriterule (.*.css$|.*.js$|.*.jpg$|.*.gif$|.*.png$) gzip.php?$1 [l]
3,上传gzip.php到根目录
4,在根目录建cache文件夹,保证可读写。
复制代码 代码如下:
<?php
/**
* @author seraphim
* @copyright 2012
*/
// <!-- 公共的返回header的子程序 -->
function sendheader($last_modified, $p_type, $content_length = 0)
{
// 设置客户端缓存有效时间
header("expires: " . gmdate("d, d m y h:i:s", time() + 15360000) . "gmt");
header("cache-control: max-age=315360000");
header("pragma: ");
// 设置最后修改时间
header("last-modified: " . $last_modified);
// 设置文件类型信息
header($p_type);
header("content-length: " . $content_length);
}
define('abspath', dirname(__file__) . '/');
$cache = true;
$cachedir = 'cache/'; //存放gz文件的目录,确保可写
if (empty($_server['query_string']))
exit();
$gzip = strstr($_server['http_accept_encoding'], 'gzip');
if (empty($gzip))
$cache = false;
$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'; //生成gz文件路径
$ext = array_pop(explode('.', $filename)); //根据后缀判断文件类型信息
$type = "content-type: text/html"; //默认的文件类型
switch ($ext)
{
case 'css':
$type = "content-type: text/css";
break;
case 'js':
$type = "content-type: text/javascript";
break;
case 'gif':
$cache = false;
$type = "content-type: image/gif";
break;
case 'jpg':
$cache = false;
$type = "content-type: image/jpeg";
break;
case 'png':
$cache = false;
$type = "content-type: image/png";
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))
{
// 与浏览器cache中的文件修改日期一致,返回304
header("http/1.1 304 not modified");
// 发送客户端header
header("content-encoding :gzip");
sendheader($gmt_mtime, $type);
}
else
{
// 读取gz文件输出
$content = file_get_contents($cache_filename);
// 发送客户端header
sendheader($gmt_mtime, $type, strlen($content));
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 = ltrim($content);
// 压缩文件内容
$content = gzencode($content, 9, $gzip ? force_gzip : force_deflate);
// 发送客户端header
sendheader($gmt_mtime, $type, strlen($content));
header("content-encoding: gzip");
// 发送数据
echo $content;
// 写入文件
file_put_contents($cache_filename, $content);
}
else
{
header("http/1.0 404 not found");
}
}
else
{ // 处理不使用gzip模式下的输出。原理基本同上
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))
{
// 与浏览器cache中的文件修改日期一致,返回304
header("http/1.1 304 not modified");
// 发送客户端header
sendheader($gmt_mtime, $type, strlen($content));
header("content-encoding :gzip");
}
else
{
// 读取文件输出
$content = file_get_contents($filename);
// 发送客户端header
sendheader($gmt_mtime, $type, strlen($content));
// 发送数据
echo $content;
}
}
else
{
header("http/1.0 404 not found");
}
}
?>
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!