定时更新静态页面的方法

2014-11-24 02:53:06 · 作者: · 浏览: 4

实现思路:
写一个程序m.php用ob调用network.php的输出(如果默认把network.php作为首页的话),把输出内容保存到 index.html,并在后面加上调用m.php的代码(<script language=java script src=m.php t=".$nowtime.">,t是更新时间,下次更新时可以先判断是否到了更新时间) .第一次需要手工执行m.php生成index.html,以后在访问index.html时会自动调用m.php更新index.html,从而实现静 态首页的自动更新代码:
Java代码
try{
<
$nowtime = time ();
$pastsec = $nowtime - $_GET ["t"];
if ($pastsec < 300) {
exit (); //5分钟更新一次,时间可以自己调整
}
ob_start (); //打开缓冲区
include ("network.php");
$content = ob_get_contents (); //得到缓冲区的内容
$content .= "\n<script language=java script src=\"m.php t=" . $nowtime . "\">"; //加上调用更新程序的代码

file_put_contents ( "index.html", $content );

if (! function_exists ( "file_put_contents" )) {
function file_put_contents($fn, $fs) {
$fp = fopen ( $fn, "w " );
fputs ( $fp, $fs );
fclose ( $fp );
}
}
>
}
catch(e){};
方法二:
PHP实现定时生成HTML网站首页
在开始之前还是提一下三个函数吧:"ob_start()、ob_end_clean()、ob_get_contents()"
ob_start():是打开缓冲区的,就是要把您需要生成的静态文件的内容缓存在这里;
ob_get_contents():是读出缓冲区里的内容,下面有代码为例;
ob_end_clean():这个比较重要,只有使用了这个函数后,缓冲区里的内容才会读取出来;
//文件修改时间和现在时间相差 的话,直接导向htm文件,否则重新生成htm
Java代码

if(file_exists("./index.htm"))//看静态index.htm文件是否存在
{
time=time();
//文件修改时间和现在时间相差 的话,直接导向htm文件,否则重新生成htm
if(time-filemtime("./index.htm")< 600)
{
header("Location:classhtml/main.htm");
}
}
//在你的开始处加入ob_start();
ob_start();
//首页内容,就是你的动态部分了
//在结尾加入ob_end_clean(),并把本页输出到一个变量中
temp=ob_get_contents();
ob_end_clean();
//写入文件
fp=fopen("./index.htm",''w'');
fwrite(fp,temp) or die(''写文件错误'');
//echo"生成HTML完成!";
FSO 是FileSystemObject 或 Scripting.FileSystemObject 的缩写,为 IIS 内置组件,用于操作磁盘、文件夹或文本文件。FSO 的对象、方法和属性非常的多.FSO判断文件最后更新时间,然后再根据时间值选择是否需要更新。
先写个获取时间的函数,方便调用
Java代码
Function GetDateLast(path)
on error resume next
Dim FSO,objFile
Set FSO = Server.CreateObject("scripting.filesystemobject")
GetDateLast = FSO.GetFile(Server.Mappath(path)).DateLastModified
Set FSO = Nothing
if err.number<>0 then err.clear
End Function
然后就是时间对比了,
Java代码
DateDiff("n", CDate(GetDateLast("文件路径")), Now())

作者“hudeyong926”