This document will give you a brief overview of the optional global.asa file you can use in Active Server Pages (ASP).
The global.asa file is an optional file that you can use in ASP to setup session and application variables and events, such as an event for when a user firsts visits the web site and initiates a new session etc. You can also use it to setup variables that can be accessed by any page in the ASP application.
Another thing worthy of note is that all browser supported scripting languages can be used within a global.asa file, including javascript.
This is possible the biggest use of the global.asa file and that is to setup custom events when someone starts a new session, (i.e. visits the web site). Normally, when a visitor visits the web site, a session is created, but nothing else is done. With global.asa you can specify events and functions that will run when a user starts a new session.
For example, you might want to store the time the session was started in the session object, or you may want to force them to a login page as soon as they visit the site etc. There are a number of possibilities.
In Global.asa, there are four events that you can use that tell the ASP application and session objects what to do when they are first created or when they are closed. These events are outlined below:
| Application_OnStart | This event is triggered when
the web server first starts up, is restarted, or when the global.asa
file is re-written/updated with a new version. |
| Session_OnStart | This event is triggered when a new user visits the web site. |
| Session_OnEnd | This event is triggered when a user closes all instances of their
web browser, or when the user has been inactive and not doing anything
and the sessions hits its time out limit. This is usually 20 minutes
on most servers unless changed by a script. |
| Application_OnEnd | This event is called when the server is shut down. |
There are no special requirements to use a global.asa file, all you need is a normal text file, although you must use the browser <SCRIPT> tags as you are not allowed to use the ASP delimited<% %>.
An example global.asa file appears below. Please note, that the global.asa file MUST be placed in the root of the web site.
<SCRIPT LANGUAGE="VBScript" RUNAT="Server">
Sub Application_OnStart
![]()
Application("ActiveUsers") = 0
End Sub
Sub Session_OnStart
Application.Lock
Application("ActiveUsers") = Application("ActiveUsers")
+ 1
Application.UnLock
End Sub
Sub Session_OnEnd
Application.Lock
Application("ActiveUsers") = Application("ActiveUsers")
- 1
Application.UnLock
End Sub
< /SCRIPT>
The above global.asa is taken from another tutorial on creating a simple web site user counter from the ASP tutorial section.
As you can see, we must use the browser <SCRIPT> tags in order to add code into the global.asa file, and we must also specify the language of the script, and that the script will be RUNAT the server end. These steps are crucial otherwise your global.asa file will not work.
In the above code, you can see that when the ASP application is first started, the global.asa file adds a global variable to the application object called ActiveUsers, and initializes it to zero.
You can also see that this global variable is updated whenever a new user visits the site, and when a users connection is closed, (either by the user closing their browser or when the users session hits its timeout limit).
If you are new to ASP, you will also want to look
up ASP Application Object, and
ASP Session Object.
Tutorial by Justin Kercher
2005.
This document may not be copied for use on another site.
Copyright© 2005.