Program | Implementation is actually using the file "Global.asa"! Many novice ASP programmers want to know what this thing is? In fact, Global.asa is an event driver that contains 4 event-handling processes: Application_OnStart, Application_OnEnd, Session_OnStart, and Session_OnEnd.
When a page of an application on a Web site is first accessed by a user, the global.asa is loaded into memory. When the application (application) starts, ends, or sessions (session) begins and ends, you can join the scripting code that needs to be executed.
With some skill, you can use the Global.asa file to achieve the purpose of the scheduled task.
Here's an example: "Track how many visitors there are, and when the traffic reaches 100, the counter resets to 0 and executes the code you've prepared." Of course, according to your own needs, you can adjust the number of visits to the upper limit of 100.
The contents of Global.asa are as follows:
< 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") > Then
Application.Lock
Application ("sessioncount") = 0
Application.UnLock
' Here's your would put any code for you need to run
' Don't surround the code with <% > tags
' For example your might run a database query that checks for expired accounts
End If
End Sub
</script >
Let's take a look at the 4-time task performed in one day. You can store the value of a date and time in a text file and check it periodically. When the time is over 6 hours, write a new date and time into this text file, and start the task you want to perform. You can change the value of "6" to perform tasks more or less.
Although the above method needs to set read and write permissions on the text file to perform correctly, this is still a very clever solution!
In this routine, the text file is checked once for each of the 15 new visitors. Of course, you can adjust the value of "15" or set the text file to be checked every time a new visitor is available. But this processing, will undoubtedly aggravate the load of the site, Waste server resources! In a word, the frequency of the check is set by yourself after consideration.
In the example, you must guarantee the legality of the date and time when the text file was first written, otherwise, a run-time error occurs when the program first reads a null value.
For example, the first line of 6/30/99 6:58:45 PM is initially written to a text file.
Currently, you can also add code to check the spelling of dates and times, and to handle error messages. But it would be simpler to write a valid date and time value initially.
The contents of Global.asa are as follows:
< 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") > 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's your would put any code for you need to run
' Don't surround the code with <% > tags
' For example your 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 better ways to achieve the purpose of the inspection, the purpose of this article is to recall the role of the trigger.
Also, keep in mind that the Web site's application must be set to a application so that the "global.asa" file will work. Many virtual domains default to application, but the underlying directories are not.
Last tip: Before you write code to Global.asa, write a test in an "ASP" file. If the test fails, it will not run successfully in Global.asa. Also, verify that the path to the text file is correct.