I searched around the Web for a current session counter to work the way I wanted it to work, but nothing was exactly right for me. So I made my own counter. In this article we'll cover some basic ways to implement a current sessions counter. The current environmet this code is running on is a Windows 2000 Server machine.
This code is executed in the global.asa file. If you remember from ASP 101 classes, the global.asa file contains four events:
the Application_OnStart event, which is triggered the first time a user access the page when the IIS service start your virtual server.
the Session_OnStart event, this is triggered for every user that first accesses your site.
the Session_OnEnd event, this is triggered after the specified period of inactivity of the individual user. Meaning if your server is set to timeout after 20 minutes of inactivity for a user, then after 20 minutes this event will be triggered.
the Application_OnEnd event, this is triggered when the virtual server or application is terminated.
These events occur for every configured IIS application on the server. Configuring IIS applications is a topic for another article.
In the following code block we have the four events in the global.asa file as defined above and one custom function that handles the session count. We have three constants defined in order to pass to the MakeCount function and let the function know what we want to do with the count.
Two events come into play here, in the Session_OnStart, when a user first hits the site I want to increment the count so I call the MakeCount subroutine and pass the appropriate parameter. Second, when a user's session expires I want to decrement the counter, so I call the MakeCount subroutine again and pass the decrement constant.
In the MakeCount subroutine all this does in add or subtract a value of one to the application("count") value, depending on what parameter was passed to the subroutine. The one check that is added to the subroutine is checking for a value of zero, in case the count gets screwed up somehow because we don't want to have a negative count..
CONST INCREMENT_COUNT = 0
CONST DECREMENT_COUNT = 1
CONST RESET_COUNT = 2
Sub Application_OnStart
End Sub
'----------------------------------------------------------------------
Sub Session_OnStart
Call MakeCount(INCREMENT_COUNT)
End Sub
'----------------------------------------------------------------------
Sub Session_OnEnd
Call MakeCount(DECREMENT_COUNT)
End Sub
'----------------------------------------------------------------------
Sub Application_OnEnd
End Sub
'----------------------------------------------------------------------
Sub MakeCount(iCountType)
On Error Resume Next
Select Case iCountType
Case RESET_COUNT
Application.Lock
Application("Count") = 0
Application.UnLock
Case INCREMENT_COUNT
Application.Lock
Application("Count") = Application("Count") + 1
Application.UnLock
Case DECREMENT_COUNT
If Application("Count") > 0 Then
Application.Lock
Application("Count") = Application("Count") - 1
Application.UnLock
End If
End Select
End Sub
Just copy the above code in your global.asa file then reference the value of the Application("count") in one of your ASP pages like the default page:
You're currently 1 of <%= Application(*"Count") %> current users.