Counting number of users logged in

  • Thread starter Thread starter Mike P
  • Start date Start date
M

Mike P

What would be the best way of counting the number of users who are
currently logged on to a website? I am making the users login against a
database of valid users. Would the best way be to add a bool field to
the table and set each user to 1 if they are logged in, and 0 if they
are not logged in?
 
What would be the best way of counting the number of users who are
currently logged on to a website? I am making the users login against a
database of valid users. Would the best way be to add a bool field to
the table and set each user to 1 if they are logged in, and 0 if they
are not logged in?

Depends if you need to know only the number of currently logged-in users, or
who they actually are...

However, what happens if e.g. 100 users log in and then all close their
browser without logging off...?
 
That is the thing I don't know how to do....I can capture which users
have clicked on a Logoff link, but how do I capture which users have
just closed the browser window, or those who have left the browser open
and whose session has timed out?
 
re:
!> how do I capture which users have just closed the browser window

You can't, unless you're using the Membership class for logins.

re:
!> those who have left the browser open

You can't.

re:
!> whose session has timed out?

You *can*, by capturing the Session_OnEnd events, but only if you're using InProc
session state management and relying on counting the number of sessions active.

If you're using SQL Server session state management or State Server,
the Session_OnEnd event ( also referred to as Session_End ) will never fire.

So, at most, you can keep track of how many sessions are active,
but not how many users are actually connected, if you rely on counting active sessions.

Btw, you didn't say whether you're using the Membership class for logins, but if you are,
you can trap the number of users logged in with Membership.GetNumberOfUsersOnline().

Simple, huh ? ...but it only works if you're using logins and Membership.




Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
======================================
 
re:
!> how do I capture which users have just closed the browser window

You can't, unless you're using the Membership class for logins.

Really..? How does it know...?
 
re:
!> Really..? How does it know...?

It keeps track of them with :

public static int GetNumberOfUsersOnline();
Declaring Type: System.Web.Security.Membership

Use Lutz Roeder's Reflector to look up System.Web.Security.Membership class's source.




Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
======================================
 
re:
!> Really..? How does it know...?

It keeps track of them with :

public static int GetNumberOfUsersOnline();
Declaring Type: System.Web.Security.Membership

Use Lutz Roeder's Reflector to look up System.Web.Security.Membership
class's source.

Yes, but how can it possibly know when a client browser has been closed...?
 
re:
!> Yes, but how can it possibly know when a client browser has been closed...?

It can't, but if a user has closed the browser, the user will still be logged in,
at least until his session times out.

It keeps track of "logged in" users, not active users, like so many counters.

Otoh, it's real easy to implement, even if it doesn't provide an accurate, real, count... ;-)




Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
======================================
 
re:
!> Yes, but how can it possibly know when a client browser has been
closed...?

It can't, but if a user has closed the browser, the user will still be
logged in,
at least until his session times out.

It keeps track of "logged in" users, not active users, like so many
counters.

Ah right - that's what I thought - you had me worried for a second there...
:-)
Otoh, it's real easy to implement, even if it doesn't provide an accurate,
real, count... ;-)

Yes indeed - I have my own implementation of that...
 
Back
Top