关于asp:asp与php中定时生成页面的思路与代码

3次阅读

共计 2096 个字符,预计需要花费 6 分钟才能阅读完成。

很多时候,咱们须要用到定时生成 html 页面的性能,原理简介:利用文件如 index.html 的最初批改工夫和以后的工夫做比拟,当时间差超过肯定距离如 2 小时,则调用相干页面进行生成新页面。原始源代码如下

PHP 版本的的定时生成页面的:

<?php
$file = dirname(__FILE__).'/index.html';
$timex=time()-filemtime($file); // 间隔时间,单位秒
if($timex>7200){ // 距离大于 2 小时,从新生成
echo "<script language=javascript src='crhtml.php'></script>";
}
?>

ASP 版本的的定时生成页面的:

<%
' 不缓存
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。

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

脚本之家原创文章,收费提供给大家了。

到此这篇对于 asp 与 php 中定时生成页面的思路与代码的文章就介绍到这了, 更多相干 asp 定时生成页面内容请搜寻思否家以前的文章或持续浏览上面的相干文章心愿大家当前多多反对思否!

正文完
 0