Active Server Pages: Global.asa



Global.asa is a text file locate in your main directory (/global.asa). Bellow is shown the basic extructure of a global.asa file.
 
global.asa
<SCRIPT LANGUAGE="VBScript" RUNAT="Server">

Sub Application_OnStart
........
End Sub

Sub Application_OnEnd
........
End Sub

Sub Session_OnStart
........
End Sub

Sub Session_OnEnd
........
End Sub

</SCRIPT>

This file will be activated in this cases:

In both cases, we may determine a series of events to be execute in the file above.
  1. Application_OnStart
    It is execute before the first session is started.
  2. Application_OnEnd
    It is execute when the application is finished.
  3. Session_OnStart
    It is execute when the server creates a new session (when a new client acccesses our server).
  4. Session_OnEnd
    It is execute when a session is abandon of after certain period of time without contact between the client and the server (normaly after 20 minutes or so from the last request from a specific client, the server will consider he is not going to come back, so it will delete all the information related to that session).


Lets try a very simple example:

Active Users Counter

Just copy the code in the table to a text file and save it in the main directory of your site ("/global.asa").
 

global.asa
<SCRIPT LANGUAGE="VBScript" RUNAT="Server">

Sub Application_OnStart
application("activevisitors")=0
End Sub

Sub Application_OnEnd
End Sub

Sub Session_OnStart
application.lock
application("activevisitors")=application("activevisitors") 1
application.unlock
End Sub

Sub Session_OnEnd
application.lock
application("activevisitors")=application("activevisitors")-1
application.unlock
End Sub

</SCRIPT>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

The first time a visitor gets to our pages, global.asa will be executed, and consequently, application("activevisitors") in line 4 will get a value equal to "0". Immediately (as a new session has started), in line 12, application("activevisitors") will be increased by one. Each time a new visitor gets to our pages application("activevisitors") will be increased by one, and identically, each time a session is finished, this parameter will be reduce by one (line 18).

In case we want to show the number of visitors in our page, we must use this kind of code :
 

index.asp
There are  <% =application("activevisitors") %> active visitors.

 

Tutorial home page