|
|
|
|
|
![]() |
||
ASP Active Visitors Counter
|
||
| Notices |









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.
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.
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.
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.
A typical "gobal.asa" file would contain the following events:
<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>
|
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. |
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!
Have Fun!