Counting number of users logged in

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?
 
M

Mark Rae [MVP]

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...?
 
M

Mike P

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?
 
J

Juan T. Llibre

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.
 
M

Mark Rae [MVP]

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...?
 
J

Juan T. Llibre

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.
 
M

Mark Rae [MVP]

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...?
 
J

Juan T. Llibre

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... ;-)
 
M

Mark Rae [MVP]

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...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top