您的位置:首页 > 其它

页面静态化思路

2013-06-09 10:09 134 查看
<?php

$_time =5;//缓存有效时间

$dir="";//当前目录下生成缓存文件,便于测试

function cache_start($_time, $dir)

{

$cachefile = $dir.sha1($_SERVER['REQUEST_URI']).'.html';

$cachetime = $_time;

ob_start();

/*-------------------------

**判断:1.缓存文件存在并且

** 2.未过期

--------------------------*/

if(file_exists($cachefile) && (time( )-$cachetime < filemtime($cachefile)))

{

include($cachefile);

ob_end_flush();

exit;

}

}

function cache_end($dir)

{

$cachefile = $dir.sha1($_SERVER['REQUEST_URI']).'.html';

$fp = fopen($cachefile, 'w');

fwrite($fp, ob_get_contents());

fclose($fp);

ob_end_flush();

}

cache_start($_time, $dir);

//以下是输出的内容,放在cache_start和cache_end两个方法之间

for($i=0;$i<=100;$i++)

{

for($j=0;$j<=$i;$j++)echo $j."-";

echo "<br>";

}

cache_end($dir);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: