PHP版本的的定時生成頁面的:
1
2
3
4
5
6
7
|
<?php $file = dirname( __FILE__ ). '/index.html' ; $timex =time()- filemtime ( $file ); //間隔時間,單位秒 if ( $timex >7200){ //間隔大于2小時,重新生成 echo "<script language=javascript src='crhtml.php'></script>" ; } ?> |
ASP版本的的定時生成頁面的:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
<% '不緩存 Response.Buffer = True Response.ExpiresAbsolute = Now() - 1 Response.Expires = 0 Response.cachecontrol = "no-cache" '讀取最后修改時間 FPath=server.mappath( "index.html" ) set fso=server.CreateObject( "scripting.filesystemobject" ) If fso.fileExists(FPath) Then Set f = fso.GetFile(FPath) crdate=f.DateLastModified end if if DateDiff( "h" ,crdate,now())>10 then '時間間隔大于一定值 response.write "<iframe border=0 frameborder=0 scrolling=no width=0 height=0 src=" "/crhtml.asp" "></iframe>" end if %> |
使用方法:在網站的流量大的頁面,一般為首頁用 iframe 調用上面的代碼即可,如插入 <iframe border=0 frameborder=0 scrolling=no width=0 height=0 src="/create.asp"></iframe>
2011-7-9 @ PS更新:正如下面留評論的朋友所說,此種方法的確會增加服務器負擔。為了避免這種方式的缺點,有2種方法來解決,
一、減少頻繁訪問被調用頁面的次數,如在流量不大的頁面調用 create.asp ;
二、直接使用 linux cron定時服務、或windows計劃任務或一些定時執行命令的小軟件 例如:hou任務計劃。
參考文章如下:
1、linux使用crontab命令定時重啟服務器
2、Cron定時執行帶參數的PHP代碼
3、Cpanel下Cron Jobs定時執行PHP的方法
這樣就可以避免頻繁調用生成判斷頁面了,只在需要執行的時候訪問一次生成頁面即可。
使用了cdn的網站需要注意的問題
鑒于現在很多網站都使用了cdn,如果不斷自動生成首頁可能導致首頁為空的情況下被cdn抓取到導致首頁是空內容,那么這樣怎么解決呢。
服務器之家的方案:例如可以生成index_def.htm,然后通過程序判斷內容是否有更新,內容是否不為空(內容一般大于30k),這樣執行復制操作將index_def.htm復制一份為index.htm即可。
winddow服務器下可以使用vbscript因為比較強大,linux可以使用shell。
原文鏈接:http://www.piaoyi.org/asp/time-create-html-code.html