Initializing variables in the global.asa file

The global.asa file won't let your VB scripts in your ASP pages store any of your variables - this is a special file which has several uses including initializing variables. The global.asa file must be located in the root directory of the web (not a subweb or folder) but the variables initialized in the file are accessible in subwebs as well as the root. One reason to initialize variables in global.asa is that if you make a change here then every page that accesses the variable will use the updated value - it is not necessary to modify each page individually. Using an 'include file' (not explained here) gives a similar result but the include file will only be accessible in one web or subweb.

If you are one of my students then you do not have access to the root web, only to a subweb, so you will not be able to change the global.asa file and this page will be of little interest to you.

This web contains a global.asa file that looks like this (some lines have been omitted):

<SCRIPT LANGUAGE=VBScript RUNAT=Server>
Sub Application_OnStart
Application("myname")="Nigel"
End Sub
Sub Session_OnStart
Session("started")=now()
End Sub
</SCRIPT>

This page you are looking at contains the VBscript (notice how double quotes must be used so that the response.write statement does not think that its corresponding string has ended before it really does):

<% response.write "The value of Application(""myname"") is " & (Application("myname")) %>
<br>
<% response.write "The value of Session(""started"") is " & (Session("started")) %>

Which has generated these lines:

The value of Application("myname") is
The value of Session("started") is

The Application_OnStart event occurs whenever the server is restarted on whenever the global.asa file is edited. The Session_OnStart event occurs whenever a user begins visiting your pages and the Session_OnEnd event occurs when the user has not loaded any of your pages in the last 20 minutes.

You can also write subroutines for the Application_OnEnd event or the Session_OnEnd event, if you want.

Note that the VBscript that makes up the global.asa file must be enclosed by the <script> tags above and not by the usual <% %> markers.


Using global.asa to count the number of visitors

Want more practice? My global.asa file also includes the following lines:

Sub Application_OnStart
Application("visitors")=0
End Sub
Sub Session_OnStart
Application.Lock
Application("visitors")=Application("visitors")+1
Application.UnLock
End Sub
Sub Session_OnEnd
Application.Lock
Application("visitors")=Application("visitors")-1
Application.UnLock
End Sub
  • The Application_OnStart subroutine sets the Application variable "visitors" to 0 when the server starts
  • The Session_OnStart subroutine adds one to the variable "visitors" every time a new visitor arrives
  • The Session_OnEnd subroutine subtracts one from "visitors" each time this subroutine is triggered

The following line, which includes some VBscript, is included in this page that you are looking at:

There are <% response.write(Application("visitors")) %> visitor(s) online now!

... and here is the line that it generated:

There are visitor(s) online now!