實現思路:
寫一個程序m.php用ob調用network.php的輸出(如果默認把network.php作為首頁的話),把輸出內容保存到 index.html,並在後面加上調用m.php的代碼(<script language=javascript src=m.php?t=".$nowtime."></script>,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=javascript src=\"m.php?t=" . $nowtime . "\"></script>"; //加上調用更新程序的代碼
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”