【发布时间】:2015-04-25 06:32:57
【问题描述】:
我一直在关注 this tutorial 的缓存功能。我遇到了为ob_start 传递回调函数cache_page() 的问题。我如何将cache_page() 以及两个参数$mid 和$path 传递给ob_start,类似于
ob_start("cache_page($mid,$path)");
以上当然行不通。下面是示例代码:
$mid = $_GET['mid'];
$path = "cacheFile";
define('CACHE_TIME', 12);
function cache_file($p,$m)
{
return "directory/{$p}/{$m}.html";
}
function cache_display($p,$m)
{
$file = cache_file($p,$m);
// check that cache file exists and is not too old
if(!file_exists($file)) return;
if(filemtime($file) < time() - CACHE_TIME * 3600) return;
header('Content-Encoding: gzip');
// if so, display cache file and stop processing
echo gzuncompress(file_get_contents($file));
exit;
}
// write to cache file
function cache_page($content,$p,$m)
{
if(false !== ($f = @fopen(cache_file($p,$m), 'w'))) {
fwrite($f, gzcompress($content));
fclose($f);
}
return $content;
}
cache_display($path,$mid);
ob_start("cache_page"); ///// here's the problem
【问题讨论】:
-
你能澄清一下
cache_pagefunction 应该做什么吗?我看到它需要三个参数,但您只在ob_start调用中传递了两个参数。同样,ob_start的回调必须具有签名string handler ( string $buffer [, int $phase ] )