帝国软件
  设为首页 加入收藏 关于我们
 
解密帝国网站管理系统
栏 目:
 
您的位置:首页 > 技术文档 > ASP编程
利用global.asp定时执行ASP
作者:hooke(转) 发布时间:2005-03-12 来源:CHINAASP.COM
Using the global.asa to schedule ASP code execution.
Have you ever had some asp code that needed to execute every once in a while but, you just didn't know how to do it.

There is a solution that doesn't involve running any scheduling or scripting software on the server and is actually very easy to get working.

You see... there is thing called the "global.asa". Most ASP newbies probably wonder what the heck it even is. The Global.asa file is event driven. It can contain four event procedures: Application_OnStart, Application_OnEnd, Session_OnStart, and Session_OnEnd.

The global.asa is basically loaded into memory the first time any user views a page on your Web application. There are event procedure stubs that can contain script you want to be executed when the application starts or ends, or when the session starts or ends.

With some tricky coding you can use this file to schedule code to execute. At least around the time you need it to, this won't be able to make it execute at exactly a certain time.

Here is the 1st example. It simply keeps track of how many visitors have been to your site and after 100 it resets the count back to 0 and executes whatever code you need to run. Obviously you'll need to adjust the "100" to whatever makes sense for the amount of visitors your site receives.

Contents of the global.asa are below.

<SCRIPT LANGUAGE=VBScript RUNAT=Server>

Sub Application_OnStart
Application("SessionCount") = 0
End Sub

Sub Session_OnStart

Application.Lock
Application("SessionCount") = Application("SessionCount") + 1
Application.Unlock

If Application("SessionCount") > 100 Then

Application.Lock
Application("SessionCount") = 0
Application.Unlock

' Here you would put any code you need to run
' do not surround the code with <% %> tags
' For example you might run a database query that checks for expired accounts

End if

End Sub

</SCRIPT>

Now lets say you want something to execute 4 times a day. You can store the date & time in a text file and check it periodically. When the date and time get to be more than 6 hours old the code will write the new date & time to the text file and then execute the code you want to run. You could change the "6" to whatever you want and therefore execute the code more or less often,

This is a pretty slick solution though it requires correct permissions to the text file for reading & writing. If not you'll get an error.

In this example we are checking the text file every 15 visitors. You can adjust this amount or remove the "check" completely so that it checks the file every time, but why check the file every time when you have a very busy site ? That would just be a waste of server resources ,but it is up to you how far you want to take this.

In this example you need to start the text file off with a valid date& time or else you will get an error because the script will read in an empty value the 1st time.

EXAMPLE: put 6/30/99 6:58:45 PM in the 1st line of the text file.

You could add code to check for that and handle the error, but I didn't really care at the time so I didn't do that. As long as there is a date there when it starts it will keep working.

Contents of the global.asa are below.

<SCRIPT LANGUAGE=VBScript RUNAT=Server>

Sub Application_OnStart
Application("SessionCount") = 0
End Sub

Sub Session_OnStart

Application.Lock
Application("SessionCount") = Application("SessionCount") + 1
Application.Unlock

If Application("SessionCount") > 15 Then

Application.Lock
Application("SessionCount") = 0
Application.Unlock

Set ObjMyFile = CreateObject("Scripting.FileSystemObject")
Set OpenMyFile = ObjMyFile.OpenTextFile(Server.MapPath("last-update.txt"))
MyFileValue = OpenMyFile.ReadLine
OpenMyFile.Close

If DateDiff("h",MyFileValue,NOW) > 6 Then

' Here you would put any code you need to run
' do not surround the code with <% %> tags
' For example you might run a database query that checks for expired accounts

Set WriteMyFile = ObjMyFile.CreateTextFile(Server.MapPath("last-update.txt"))
WriteMyFile.WriteLine(NOW)
WriteMyFile.Close

End if
End If

End Sub

</SCRIPT>


Please Note: There are many ways to make this better and many different possible variations of what to check for before executing the desired code. This article should get you started and give you some ideas.

Also remember that if the web is not set up to run as an application the "global.asa" will not run. You'll need to make sure the web is an application. Most Virtual Domains are by default, but sub webs usually are not.

For the sub webs to run the 'global.asa" they need to be an application as the root usually is. In NT this is accomplished via the Internet Service Manager under the properties of the sub web you want to make an application.

Here is what it looks like in IIS4.

One Last Thing: Before you put any code in your Global.asa to execute during events test it first in a regular ".asp" page. If it doesn't run there it sure isn't going to run in your global.asa. Also make sure you file paths are correct for the text file. Everything has to be perfect for this sort of thing to work.

Good Luck

  
评论】【加入收藏夹】【 】【打印】【关闭
※ 相关链接
 ·如何利用資料庫儲存圖檔  (2005-03-12)
 ·利用global.asa计划执行程序  (2005-03-12)
 ·IIS5 UNICODE 编码漏洞的利用心得  (2005-03-12)
 ·IIS5 UNICODE 编码漏洞的利用心得  (2005-03-12)
 ·利用JS在页面上动态生成直线  (2005-03-12)
 ·利用XML不离开页面刷新数据  (2005-03-12)
 ·利用CDONTS发送邮件的ASP函数  (2005-03-12)
 ·利用ASP在线维护数据库  (2005-03-12)
 ·在ASP中利用Oracle Object for O  (2005-03-12)
 ·利用ASP远程注册DLL的方法  (2005-03-12)

   栏目导行
  PHP编程
  ASP编程
  ASP.NET编程
  JAVA编程
   站点最新
·致合作伙伴的欢迎信
·媒体报道
·帝国软件合作伙伴计划协议
·DiscuzX2.5会员整合通行证发布
·帝国CMS 7.0版本功能建议收集
·帝国网站管理系统2012年授权购买说
·PHPWind8.7会员整合通行证发布
·[官方插件]帝国CMS-访问统计插件
·[官方插件]帝国CMS-sitemap插件
·[官方插件]帝国CMS内容页评论AJAX分
   类别最新
·在ASP中使用数据库
·使用ASP脚本技术
·通过启动脚本来感受ASP的力量
·学习使用ASP对象和组件
·解析asp的脚本语言
·初看ASP-针对初学者
·ASP开发10条经验总结
·ASP之对象总结
·ASP与数据库应用(给初学者)
·关于学习ASP和编程的28个观点
 
关于帝国 | 广告服务 | 联系我们 | 程序开发 | 网站地图 | 留言板 帝国网站管理系统