使用PHP输出缓冲区压缩缓存输出
通过使用这一行代码使用PHP输出缓冲区压缩缓存输出,php,html,caching,output-buffering,ob-start,Php,Html,Caching,Output Buffering,Ob Start,通过使用这一行代码ob_start('ob_gzhandler')
ob_start('ob_gzhandler')根据Chrome控制台,页面顶部的php输出大约为11KB。当我试图用下面的代码缓存输出时,我发现缓存的文件保存了大约65kb。更大的输出大小是否是缓存的折衷方案?有没有办法进一步压缩缓存的输出?我尝试过为html压缩添加一些htaccess规则,但我认为这没有帮助
$id = $_GET["id"];
$cachefile ="cache/p_{$id}.html";
if (file_exists($cachefile)) {
include($cachefile);
echo "<!-- Cached ".date('jS F Y H:i', filemtime($cachefile))." -->";
exit;
}
ob_start('ob_gzhandler');
$fp = fopen($cachefile, 'w'); // open the cache file for writing
fwrite($fp, ob_get_contents()); // save the contents of output buffer to the file
fclose($fp); // close the file
ob_end_flush();
$id=$\u GET[“id”];
$cachefile=“cache/p{$id}.html”;
如果(文件_存在($cachefile)){
包括($cachefile);
回声“;
出口
}
ob_start('ob_gzhandler');
$fp=fopen($cachefile,'w');//打开缓存文件进行写入
fwrite($fp,ob_get_contents());//将输出缓冲区的内容保存到文件中
fclose($fp);//关闭文件
ob_end_flush();
服务器未对缓存文件进行gzip处理,请尝试以下方法:
ob_start('ob_gzhandler');
$id=$_GET[“id”];
$cachefile=“cache/p{$id}.html”;
如果(文件_存在($cachefile)){
包括($cachefile);
回声“;
}否则{
//你的html或其他东西。。。
$fp=fopen($cachefile,'w');//打开缓存文件进行写入
fwrite($fp,ob_get_contents());//将输出缓冲区的内容保存到文件中
fclose($fp);//关闭文件
}
ob_end_flush();
p、 我会把压缩任务留给web服务器(,)。@RedGiant:或者像你建议的那样保存已经压缩的文件。这完全取决于你的需要。现在,当您保存了gzip缓存文件时,必须将encoding设置为gzip,以便浏览器知道数据是什么:如果(file_存在($cachefile)){header('Content-encoding:gzip');…
。