asp與php中定時(shí)生成頁(yè)面的思路與代碼
PHP版本的的定時(shí)生成頁(yè)面的:
<?php $file = dirname(__FILE__)."/index.html"; $timex=time()-filemtime($file); //間隔時(shí)間,單位秒 if($timex>7200){ //間隔大于2小時(shí),重新生成 echo "<script language=javascript src="crhtml.php"></script>"; } ?>
ASP版本的的定時(shí)生成頁(yè)面的:
<% "不緩存 Response.Buffer = True Response.ExpiresAbsolute = Now() - 1 Response.Expires = 0 Response.cachecontrol = "no-cache" "讀取最后修改時(shí)間 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 "時(shí)間間隔大于一定值 response.write "<iframe border=0 frameborder=0 scrolling=no width=0 height=0 src=""/crhtml.asp""></iframe>" end if %>
使用方法:在網(wǎng)站的流量大的頁(yè)面,一般為首頁(yè)用 iframe 調(diào)用上面的代碼即可,如插入 <iframe border=0 frameborder=0 scrolling=no width=0 height=0 src="/create.asp"></iframe>
2011-7-9 @ PS更新:正如下面留評(píng)論的朋友所說(shuō),此種方法的確會(huì)增加服務(wù)器負(fù)擔(dān)。為了避免這種方式的缺點(diǎn),有2種方法來(lái)解決,
一、減少頻繁訪問(wèn)被調(diào)用頁(yè)面的次數(shù),如在流量不大的頁(yè)面調(diào)用 create.asp ;
二、直接使用 linux cron定時(shí)服務(wù)、或windows計(jì)劃任務(wù)或一些定時(shí)執(zhí)行命令的小軟件 例如:hou任務(wù)計(jì)劃。
參考文章如下:
1、linux使用crontab命令定時(shí)重啟服務(wù)器
2、Cron定時(shí)執(zhí)行帶參數(shù)的PHP代碼
3、Cpanel下Cron Jobs定時(shí)執(zhí)行PHP的方法
這樣就可以避免頻繁調(diào)用生成判斷頁(yè)面了,只在需要執(zhí)行的時(shí)候訪問(wèn)一次生成頁(yè)面即可。
使用了cdn的網(wǎng)站需要注意的問(wèn)題
鑒于現(xiàn)在很多網(wǎng)站都使用了cdn,如果不斷自動(dòng)生成首頁(yè)可能導(dǎo)致首頁(yè)為空的情況下被cdn抓取到導(dǎo)致首頁(yè)是空內(nèi)容,那么這樣怎么解決呢。
的方案:例如可以生成index_def.htm,然后通過(guò)程序判斷內(nèi)容是否有更新,內(nèi)容是否不為空(內(nèi)容一般大于30k),這樣執(zhí)行復(fù)制操作將index_def.htm復(fù)制一份為index.htm即可。
winddow服務(wù)器下可以使用vbscript因?yàn)楸容^強(qiáng)大,linux可以使用shell。
vbscript
Dim fso Set fso = CreateObject("Scripting.FileSystemObject") f1="F:\webroot\jb51net\index_def.htm" f2="F:\webroot\jb51net\index.htm" fsize=50000 "50k set fn2=fso.GetFile(f1) flsize2=fn2.size fldate2=fn2.datelastmodified set fn=fso.GetFile(f2) flsize1=fn.size fldate1=fn.datelastmodified If fso.FileExists(f1) and flsize2>fsize and fldate2>fldate1 Then fso.getfile(f1).copy(f2) if err.number=0 then WriteHistory "成功"&now()&".........","log.txt" end if Sub WriteHistory(hisChars, path) Const ForReading = 1, ForAppending = 8 Dim fso, f Set fso = CreateObject("Scripting.FileSystemObject") Set f = fso.OpenTextFile(path, ForAppending, True) f.WriteLine hisChars f.Close End Sub
原創(chuàng)文章,免費(fèi)提供給大家了。
到此這篇關(guān)于asp與php中定時(shí)生成頁(yè)面的思路與代碼的文章就介紹到這了,更多相關(guān)asp定時(shí)生成頁(yè)面內(nèi)容請(qǐng)搜索以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持!
相關(guān)文章:
1. ASP中解決“對(duì)象關(guān)閉時(shí),不允許操作。”的詭異問(wèn)題……2. msxml3.dll 錯(cuò)誤 800c0019 系統(tǒng)錯(cuò)誤:-2146697191解決方法3. 得到XML文檔大小的方法4. ASP使用MySQL數(shù)據(jù)庫(kù)的方法5. WMLScript的語(yǔ)法基礎(chǔ)6. ASP動(dòng)態(tài)網(wǎng)頁(yè)制作技術(shù)經(jīng)驗(yàn)分享7. xml中的空格之完全解說(shuō)8. html小技巧之td,div標(biāo)簽里內(nèi)容不換行9. XML入門(mén)的常見(jiàn)問(wèn)題(四)10. ASP中if語(yǔ)句、select 、while循環(huán)的使用方法
