Thursday, April 3, 2008

One of the more common questions I've found on the net is 'How do you show
the number of current users browsing my site?", like ASPNet101.com has on the
bottom left of each page. This tutorial will take the mystery out of this by
breaking it down, piece by piece.


It all starts with the Global.asax file. For those of you who have come from
the Classic ASP world, you will probably recognize the name of this file, since
Classic ASP also had a Global.asa file. Here, we track the active Sessions for
our application. There are three subroutines into which we'll be looking to do
this - Application_OnStart, Session_OnStart, and the Session_OnEnd.


First, in the Application_OnStart sub, we basically set the user count to 0,
when the server starts



Sub Application_OnStart (Sender as Object, E as EventArgs)
' Set our user count to 0 when we start the server
Application("ActiveUsers") = 0
End Sub


Next, in the Session_OnStart subroutine, there are several things happening:



Sub Session_OnStart (Sender as Object, E as EventArgs)
Session.Timeout = 10
Session("Start") = Now
Application.Lock
Application("ActiveUsers") = Cint(Application("ActiveUsers")) + 1
Application.UnLock
End Sub


The first thing is the Timeout - you don't need to put anything here, but,
the default Timeout is 20 minutes, so you can change or add it, depending on the
needs of your particular application.


To set the session start time, we add (Session("Start") = Now).
Basically, when the user hits the site and opens a web page (asp.net), at the
time he/she opens the page, the session starts. Next, we increase the active
visitors count when we start the session (Application("ActiveUsers") =
Cint(Application("ActiveUsers")) + 1
). The Application lock & unlock
adds more stability to the counting.


Next, we must decrement the number of online sessions in the
Session_OnEnd
subroutine:



Sub Session_OnEnd(Sender as Object, E as EventArgs)
Application.Lock
Application("ActiveUsers") = Cint(Application("ActiveUsers")) - 1
Application.UnLock
End Sub


As you can see, it's pretty much the same code as adding to the current
session count - Except - we subtract one from the count, decreasing the
active visitors count when the session ends.


The last thing to do is to display the count on the page. In this site, I
have a user control that shows up on the left side of every page, which does
this. Including it in a user control makes it much easier to maintain, since it
only needs to be changed in one place and not on every page. As you can see
here, very little code is actually necessary to show the current session/user
count:



<% 
Dim intNumber as Integer
intNumber =Application("ActiveUsers")
response.write (intNumber )
%> Currently Online


Of course, you can use any html tags you'd like to format this in exactly the
way you'd like for your site.


So now, you can see that the tracking and displaying the number of current
sessions/users is a very simple process and can be implemented with little or no
fuss on your own site.

No comments: