利用global.asp定时执行ASP

2016-01-29 18:22 29 1 收藏

利用global.asp定时执行ASP,利用global.asp定时执行ASP

【 tulaoshi.com - ASP 】

  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(

来源:http://www.tulaoshi.com/n/20160129/1505480.html

延伸阅读
我们知道,想在一个操作系统执行任何一个操作都是需要用户的。然后操作系统会检查该用户是否有权限执行这个操作的权限,如果有,就会执行,如果没,就会拒绝。这和我们浏览网站是一样的道理,当我们在客户端输入一串URL并执行时,其实是向服务端发送要浏览的请求,在操作系统来看,它也是一个操作,也是需要用户的。以windows为例,它是用:...
标签: ASP
  2.影响ASP的要素 (1).尽量把对象变量转换成本地变量,因为读本地变量比读对象变量要快许多。 比较慢的例子: if Myobj.Value = 0 then Do something elseif Myobj.Value > 0 then Do something elseif Myobj.Value < 0 then Do something end if 比较快的例子: MyVar = Myobj.Value if MyVar = 0 then D...
标签: Web开发
页面执行时间:就是从这页的开始执行一直到这页执行完毕所用的时间。 许多网站的首页的页尾都会显示一个页面执行时间,下面说说如何实现: 首先在一个网页的开头定义一个变量: dim startime startime=timer() 在显示页面执行时间的地方,这个地方应该是页尾的地方: dim endtime endtime=timer() ...
标签: Web开发
如想实现 http://school.cnd8.com/hello.zw  同样用ASP的语法来执行,我们还需要一些额外的工作。 为了让对后缀名为.zw的文件的请求能够被我们的Handler截获运行 打开IIS的管理控制台,又键单击站点,选择"属性",跳出站点的属性对话框。选择主目录选项,配置。 添加映射可执行文件里可以复制.asp里的C:WINDOWSsystem3...
标签: ASP
  <% set conn=server.createobject("adodb.connection") conn.open application("connstring") set rs=server.createobject("Adodb.recordset") sql="select * from xxloginteachers order by jsid desc" rs.open sql,conn do while not rs.eof msg=msg & rs("jsid") & ch...

经验教程

73

收藏

99
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部