没有合适的资源?快使用搜索试试~ 我知道了~
PHP版网站缓存加快打开速度的方法分享
1 下载量 184 浏览量
2020-12-19
10:43:37
上传
评论
收藏 47KB PDF 举报
温馨提示
试读
3页
说明: 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版网站缓存加快打开速度的方法分享版网站缓存加快打开速度的方法分享
说明:
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;
weixin_38744778
- 粉丝: 7
- 资源: 917
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0