ASP Code

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 Comments

  1. 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. 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

Your email address will not be published. Required fields are marked *

*
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.
Anti-spam image