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!
2 Comments
Thank you but when I used it in ASP.net 2 it gives me error, and the correct is:
There are currently users online!
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!