频道直达 - 学院 - 下载 - 交易 - 特效 - 字库 - 手册 -排名-工具- 繁體
网页教学网站开发 设为首页
加入收藏
联系我们
建站搜索: 常用广告代码   用户注册 | 用户登陆
您当前的位置:中国建站之家 -> 网站开发设计技术教程 -> asp教程 -> 利用global.asp定时执行ASP

利用global.asp定时执行ASP

作者:未知  来源:转载  发布时间:2005-7-28 14:35:50  发布人:acx

减小字体 增大字体

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


将本文收藏到QQ书签与更多好友分享
[打 印]
[] [返回上一页] [收 藏]
∷相关文章评论∷    (评论内容只代表网友观点,与本站立场无关!) [更多评论...]
精彩推荐
热门文章
· 注册码大全二
· 注册码大全四
· 注册码大全一
· 要10G免费网络硬盘的请进..
· 通过google 赶快来赚美金..
· 注册码大全十
· 头像-qq头像(qq新头像)4..
· 让你轻松架设FTP服务器1..
· 注册码大全三
· 梦幻背景图片7
· 卡通动物图片6
· 网页制作素材-按钮素材2..
· 让你轻松架设FTP服务器5..
· 风景图片8
· 注册码大全九
· 让你轻松架设FTP服务器2..
关注此文读者还看过
· 天下没有免费的午餐
· 从HTML中脱离
· 如何使用asp创建dsn?
· 奇虎360安全卫士可杀木马..
· 反流氓软件再起波澜 雅虎..
· 强制弹出页面+自动最小化..
· 谈谈网站的网络营销
· ASP字数计算函数
· ASP Image 使用范例
· 文件删除函数
· 用 jsfl 扩展你的 flash..
· 全面剖析VB.NET(2)
· 导航条按钮控制对象
· 简单快捷实现ASP在线发邮..
· 用密码保护页面 (I)
· 从头创建 Visual Basic ..
相关文章
· 不使用VS进行ASP.NET Membe..
· 简单几行ASP便可生成无限英..
· 用几行Asp代码实现防止表单..
· 如何准确定时运行ASP文件
· 利用global.asa计划执行程序..
· 用InstallShield9 进行ASP软..
· 如何定时运行ASP文件(转载)..
关于本站 - 网站帮助 - 广告合作 - 下载声明 - 友情连接 - 网站地图 - 人才招聘
网站合作、内容监督、商务咨询:QQ: 9576619
Copyright ? 2005--2008 中国建站之家版权所有
粤ICP备05092265号