用缓存实现静态页面的测试

林继 PHP 技术 5,463 共写了1374个字 (2005-12-08 18:40:49) 没有评论 打印 扫描二维码 百度未收录

出处:blog.chinaunix.net

一段简单的代码,实现静态页面的生成。尽管如此,它体现的是一种思想。

function CreateShtml()

{

ob_start(array(“callback_CreateShtml”,”callback_GoToShtml”));

}

function callback_CreateShtml($buffer)[

{

$page = intval(@$_REQUEST[“page”]);

$fileName = $_SERVER['DOCUMENT_ROOT'] . dirname($_SERVER['PHP_SELF']) . “/” . basename($_SERVER['PHP_SELF'],”.php”) . ($page==0 ? “” : “_” . strval($page)) . “.htm”;

$fp = fopen($fileName,”wb”);

fwrite($fp,$buffer);

fclose($fp);

return $buffer;

}

function callback_GoToShtml($buffer)

{

$page = intval(@$_REQUEST[“page”]);

$fileName = basename($_SERVER['PHP_SELF'],”.php”) . ($page==0 ? “” : “_” . strval($page)) . “.htm”;

header(“location:” . $fileName);

return $buffer;

}

function GoToShtml()

{

$page = intval(@$_REQUEST[“page”]);

$fileName = basename($_SERVER['PHP_SELF'],”.php”) . ($page==0 ? “” : “_” . strval($page)) . “.htm”;

if(file_exists($fileName))

header(“location:” . $fileName);

}

function DeleteShtml($fileName=NULL)

{

if(is_null($fileName))

$fileName = $_SERVER['DOCUMENT_ROOT'] . $_SERVER['PHP_SELF'];

if($fileName[0]==”/”)

$fileName = $_SERVER['DOCUMENT_ROOT'] . $fileName;

$path = dirname($fileName);

$dir = dir($path);

$patten = “/^” . basename($fileName, “.php”) . “(_[0-9]+)?.htm/”;

while(($entry = $dir->read())!==false)

{

if(is_file($path . “/” .$entry) && preg_match($patten,$entry))

unlink ($path . “/” . $entry);

}

}

CreateShtml();
echo “这是静态生成的页面!$page”;
GoToShtml();
DeleteShtml();
?>

如果觉得我的文章对您有用,请随意赞赏。您的支持将鼓励我继续创作!

发表评论

电子邮件地址不会被公开。 必填项已用*标注

< >