Webforumz Newsletter - January 2008
Tutorials
ASP Active Visitors Counter – How many people are using your web site at any particular time?
The Global.ASA file:
In this quick little tutorial, we are going to take a look at building a routine that checks to see how many people are using your web site at any particular time. In order to achieve this, we are going to use something called the "global.asa" file.
What is the global.asa file?
As you all probably know, "ASP" stands for "Active Server Pages". We use ASP pages to execute script on the server enabling us to interact with databases and create dynamic content for our web site. "ASA" on the other hand stands for "Active Server Application".
The "global.asa" file is created in order to contain scripts that can be accessed "globally" by the entire web site.
The Global file is a file in which (amongst other things) you can specify event scripts and declare objects that have session or application wide scope. It is not a content file and nothing in it is displayed directly to the users; instead it stores event information and objects used "globally" by the application.
- The file must be named "global.asa"
- It must be stored in the root directory your application/website ("/global.asa").
- Your application can only have one global.asa file.
What are Applications and Sessions?
Application:
As mentioned above, an application (in ASP terms) refers to your asp web site. An "Application" Starts when the web server is switched on and Ends when the web server stops.
Session:
A "Session" Starts from the time a visitor starts looking at your site and Ends (in theory) when the visitor leaves your site. In practice though, a session has a 20 minute default "timeout". In order to keep track of the session a cookie is created for the visitor. This cookie is deleted when the user closes his browser. All session data is stored on the server.
Global.asa file content.
A typical "gobal.asa" file would contain the following events:
- Application_OnStart
Is executed when the server is switched on and before the first session is started. - Application_OnEnd
Is executed when the application is finished (i.e. the server is switched off). - Session_OnStart
Is executed when the server creates a new session (i.e. when a new client accesses your web site). - Session_OnEnd
Is executed when a session is abandoned or after certain period of inactivity between the visitor and the server (by default after a lapse of 20 minutes from the last request from a visitor the server will consider he is not going to come back and will delete any information related to that session).
Script for the global.asa file:
<Script Language="VBScript" RUNAT="Server">
Sub Application_OnStart
Application( "WhosOnline" ) = 0
End Sub
Sub Application_OnEnd
End Sub
Sub Session_OnStart
Application.Lock
Application( "WhosOnline" ) = Application( "WhosOnline" )+1
Application.UnLock
End Sub
Sub Session_OnEnd
Application.Lock
Application( "WhosOnline" ) = Application( "WhosOnline" )-1
Application.UnLock
End Sub
</Script>
What this script does...
1 |
<Script Language="VBScript" RUNAT="Server"> |
Opens the script. Note that you must never use the ASP <% %> delimiters in a global.asa file |
2 |
||
3 |
Sub Application_OnStart |
Sets up the Application subroutine when the server starts |
4 |
Application( "WhosOnline" ) = 0 |
Names the Application "WhosOnline" and sets initial value to zero. |
5 |
End Sub |
Closes the Application subroutine set up procedure. |
6 |
||
7 |
Sub Application_OnEnd |
Closes the Application subroutine in the event of the server closing down |
8 |
End Sub |
|
9 |
||
10 |
Sub Session_OnStart |
Sets up a subroutine for when a session opens. |
11 |
Application.Lock |
Locks access so that only 1 person at a time can write to a session. |
12 |
Application( "WhosOnline" ) = Application( "WhosOnline" )+1 |
Increments the count by 1 every time a new session is created |
13 |
Application.UnLock |
Unlocks the application so that the next visitor can create a session. |
14 |
End Sub |
Ends that section of the subroutine. |
15 |
||
16 |
Sub Session_OnEnd |
Sets up a subroutine for when a session closes |
17 |
Application.Lock |
Locks access so that only 1 person at a time can write to the session. |
18 |
Application( "WhosOnline" ) = Application( "WhosOnline" )-1 |
decreases the count by 1 every time a session is closed. |
19 |
Application.UnLock |
Unlocks the application |
20 |
End Sub |
Ends this section of the subroutine. |
21 |
||
22 |
</Script> |
Closes the script. |
Script for displaying the results on a web page...
Displaying the results on a page is simply a case of adding this 1 line of code to the area on your page where you want the number to be displayed.
There are currently <%=Application("WhosOnline")%> active visitors!
See how simple it is in retrieving the value of your active visitors counter stored in the application object. Now, every time a visitor comes to your .asp application / homepage you can see the actual number of visitors in your page. Also, you can extend the code above to use images of numbers as your counter display.
You could even store the information in your database for your site statistic. Maybe that will be a future tutorial. Well, that's all for now. Don't forget to save your page as an ASP page ("yourpage.asp").
And that's about it really. Just a few things to remember!
- You can only have 1 global.asp file for your web site but that file can contain more than 1 sub routine.
- The global.asp file must live in the root folder for your site.
- If you already have a global.asp for your site simply add the code we have been discussing to the file.
- If you are embarrassed by the lack of people on your site at any particular time you can cheat by changing the "0" in line 4 to a higher starting number.
Have Fun!