PHP版网站缓存加快打开速度的方法分享

yipeiwu_com5年前PHP代码库
说明:
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");
}
}
?>

相关文章

php自定义hash函数实例

本文实例讲述了php自定义hash函数实现方法。分享给大家供大家参考。具体分析如下: 这里演示php实现的一个简单hash算法,可以用来加密,不过这个函数过于简单,不能用来解密 fu...

javascript数组与php数组的地址传递及值传递用法实例

本文实例讲述了javascript数组与php数组的地址传递及值传递用法。分享给大家供大家参考。具体如下: javascript数组为地址传递/引用传递,而php数组为值传递 实例代码如...

PHP实现生成数据字典功能示例

PHP实现生成数据字典功能示例

本文实例讲述了PHP实现生成数据字典功能。分享给大家供大家参考,具体如下: 最近时间紧迫,没有时间发博客,趁现在有点时间向大家分享一个知识点。在咱们做开发的时候 ,也许经常会遇到对数据库...

PHP-FPM实现性能优化

简介: PHP-FPM 是一个 PHP FastCGI 管理器,一般 Nginx 上面跑 PHP 程序都会将 PHP 程序丢给 PHP-FPM 来解析。好了,就这样! PHP 5.4 开...

Drupal 添加模块出现莫名其妙的错误的解决方法(往往出现在模块较多时)

状况如下: 1、点击保按钮后,不能正常转到页面,出现空白页面; 2、刷新页面,发出导航中许多项已经没有了,还存在的项点击也不能进入正常的页面。 测试了多次没有发现任何问题,多次重装、测试...