Sections
     
     
WWWCoder.com Resource Directory

Creating an ASP Session Counter
9/9/2000 11:49:35 AM

I searched around the Web for a current session counter to work the way I wanted it to work, but nothing was exactly the right way for me. So I rolled my own. In this article we'll cover some basic ways to implement a current sessions counter.

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.


Page Options:
format for printing  Format for Printer
email article  Email Page
add to your favorites   Add to Favorites
How would you rate the quality of this content?
Poor - - Excellent
Comments?
Overall Rating:
Comments Left:
Left on 6/9/2009 6:57:42 PM by Anonymous
Comments:
Left on 4/8/2008 6:14:11 AM by Anonymous
Comments: not well
Left on 4/8/2008 6:12:21 AM by Anonymous
Comments: super
Left on 3/8/2007 10:48:05 AM by Anonymous
Comments: not good
Left on 3/3/2005 6:03:14 PM by Anonymous
Comments: Unfortunately, Session_OnEnd fires if your code ever changes the user's timeout, using, say, Session.TimeOut = 10 minutes.  This will decrement the number when it shouldn't.  :-(
No ratings available.
Left on 1/27/2005 11:00:07 AM by Anonymous
Comments: then, based on this create one yourself!
"very well but what if a tsunami arrives where is your host"
Left on 1/8/2005 4:43:26 AM by Anonymous
Comments: very well but what if your host as global.asa turned off
No ratings available.
Left on 1/13/2004 3:19:52 AM by Anonymous
Comments: GOOD
     
     

 

 

     
     

 


 


Digg This
 


DotNetNuke Platinum Benefactor

 


 


Digg This
 


DotNetNuke Platinum Benefactor

     
     

Other family network sites: santry.com - katieandkarleigh.com

Powered by 

 

     
Copyright 20010 - Santry Technology Solutions, Box 172, Girard, PA 16417, (814) 774-0970
Privacy Statement | Terms Of Use