Getting number of active sessions (online users counter) with ASP.NET

All that have to be done is in global.asax file:

Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)

Application("OnlineUsers") = 0

End Sub

 

Sub Application_End(ByVal sender As Object, ByVal e As EventArgs)

End Sub

Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)

End Sub

Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)

Application.Lock()

Application(
"OnlineUsers") = CInt(Application("OnlineUsers")) + 1

Application.UnLock()

End Sub

Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)

Application.Lock()

Application(
"OnlineUsers") = CInt(Application("OnlineUsers")) - 1

Application.UnLock()

End Sub

And to get the counter value in any form you want just call Response.Write(Application("OnlineUsers")) or Label1.Text = Application("OnlineUsers") whatever you feel comfortable.

It will show all open sessions.

Application.Lock() is used to lock down the application state values for editing only from one person at a time and Application.UnLock() is unlocking the state for edit from somebody else.

 

Hope it helps

Published Friday, June 26, 2009 7:43 AM by stoian bucovich

Comments

# re: Getting number of active sessions (online users counter) with ASP.NET

Thursday, September 23, 2010 5:28 PM by Marc Selman

Very nice! Thank you.

Is it true that every crawler request increments this number? And is there anything to counter that?

# re: Getting number of active sessions (online users counter) with ASP.NET

Wednesday, July 27, 2011 2:12 PM by Stoian Bucovich

No not realy because the crawlers dosn't accept cookies where actualy asp.net set its session cookie. :)

# Stoian bucovich | Alliterationpl

Saturday, November 26, 2011 3:50 PM by Stoian bucovich | Alliterationpl

Pingback from  Stoian bucovich | Alliterationpl

Leave a Comment

(required) 
(required) 
(optional)
(required)