欢迎光临
我们一直在努力

利用global.asp定时执行ASP

建站超值云服务器,限时71元/月

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 didnt know how to do it.

there is a solution that doesnt 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 wont 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 youll 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 youll 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 didnt really care at the time so i didnt 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. youll 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 doesnt run there it sure isnt 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

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 利用global.asp定时执行ASP
分享到: 更多 (0)