October 6th, 2006

Track and display visitor count in globa.asa with ASP

It is very easy within ASP to track and display the total number of visitors to your web application. Using the global.asa file, which is automatically included and processed in each webapp, we can create some simple session variables to track visitors.


<script language="vbscript" runat="server">

Sub Application_OnStart
       'Set the active users to zero at the time of starting or resetting the application
       Application("ActiveUsers") = 0
End Sub

Sub Session_OnStart
       ' a new person is visiting, so start a new session
       Session.Timeout = 20
       Session("Start") = Now
       Application.Lock

       'Add one to the active users when the person first visits
       Application("ActiveUsers") = Application("ActiveUsers") + 1

       Application.unlock
End Sub

Sub Session_OnEnd
       ' the user's session has timed out, so they most likely left
       Application.Lock

       'Subtract one from the number os users
       Application("ActiveUsers") = Application("ActiveUsers") - 1

       Application.unlock
End Sub

</SCRIPT>

To display the current total of users on the site, include the following code any where on your ASP pages.


There are currently <% = Application("ActiveUsers") %> users online!
Share

2 Responses to ' Track and display visitor count in globa.asa with ASP '

Subscribe to comments with RSS or TrackBack to ' Track and display visitor count in globa.asa with ASP '.

  1. Hany Galal said,

    on December 15th, 2007 at 4:42 pm

    Thank you but when I used it in ASP.net 2 it gives me error, and the correct is:
    There are currently users online!

  2. Hany Galal said,

    on December 15th, 2007 at 4:46 pm

    Thank you but when I used it in ASP.net 2 it gives me error, and the correct is:

    There are currently <% = Application["ActiveUsers"] %> users online!

Leave a reply

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word