【发布时间】:2016-10-07 20:30:46
【问题描述】:
我正在尝试在 PHP 中创建页面缓存。
首先,当我添加此行 ob_start('ob_gzhandler') 以打开输出缓冲时,我的 firefox 浏览器将显示此错误
内容编码错误
您尝试查看的页面无法显示 因为它使用了无效或不受支持的压缩形式。请 联系网站所有者,告知他们这个问题。
在 chrome 上我得到这个错误
无法访问此网站
网页http://example.conm/index 可能暂时关闭,或者它可能已永久移动到新的 网址。 ERR_CONTENT_DECODEDING_FAILED
这是我在运行之前使用的代码,但我不知道出了什么问题。
<?php
$dynamiccatch = true;
//Catch method Enable this function in Define.php
if($dynamiccatch == true && (!isset($_SESSION['username'])) && (!isset($_SESSION['cjladmin']))){
$cache_ext = '.html'; //file extension
$cache_time = 3600; //Cache file expires after these seconds (1 hour = 3600 sec)
$cache_folder = 'cache/'; //folder to store Cache files
$ignore_pages = array('', '');
$dynamic_url = 'http://'.$_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] . $_SERVER['QUERY_STRING']; // requested dynamic page (full url)
$cache_file = $cache_folder.md5($dynamic_url).$cache_ext; // construct a cache file
$ignore = (in_array($dynamic_url,$ignore_pages))?true:false; //check if url is in ignore list
if (!$ignore && file_exists($cache_file) && time() - $cache_time < filemtime($cache_file)){ //check Cache exist and it's not expired.
ob_start('ob_gzhandler'); //Turn on output buffering, "ob_gzhandler" for the compressed page with gzip.
readfile($cache_file); //read Cache file
echo '<!-- cached page - '.date('l jS \of F Y h:i:s A', filemtime($cache_file)).', Page : '.$dynamic_url.' -->';
ob_end_flush(); //Flush and turn off output buffering
exit(); //no need to proceed further, exit the flow.
}
//Turn on output buffering with gzip compression.
ob_start('ob_gzhandler');
######## End catch technology #########
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title><?php echo 'Index | '.WebName;?></title>
<?php include('meta.php');?>
</head>
<body>
My website here
</body>
</html>
<?php
if($dynamiccatch == true && (!isset($_SESSION['username'])) && (!isset($_SESSION['cjladmin']))){
######## Catch footer ends here #########
if (!is_dir($cache_folder)) { //create a new folder if we need to
mkdir($cache_folder);
}if(!$ignore){
$fp = fopen($cache_file, 'w'); //open file for writing
fwrite($fp, ob_get_contents()); //write contents of the output buffer in Cache file
fclose($fp); //Close file pointer
}ob_end_flush(); //Flush and turn off output buffering
}
?>
【问题讨论】:
-
请修正您的代码格式
-
@VuralAcar 这是我的问题,我已经尝试让它穿上超过 24 小时,但仍然不知道该怎么做
-
我确定是
utf-8 file内部有UTF-8 BOM的问题 -
@VuralAcar 我认为是真的,我删除了代码顶部的所有内容,所以让我弄清楚它来自哪里
-
你在使用 Notepad++ 吗?
标签: php html cache-control