How to track the total number of logged in members

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jeephp
Forum Newbie
Posts: 16
Joined: Sat Apr 23, 2005 4:42 am

How to track the total number of logged in members

Post by jeephp »

I am currently working on a membership based web site. I need to display the number of members logged in that system. To do this I have set a flag in the database which is set to 1 whenever a user is logged in the system and when the user clicks on the ‘Log Out’ link a query is fired that sets the same flag to 0. To display the total number of logged in members I run a query that counts the number of records whose flag is set to 1.

However this method has a major shortcoming. When users do not click the ‘Log Out’ link but just close the browser. In such a situation the query that will set the flag field back to 0 will bot be fired and hence the user would be seen as active in the system even though the user has already left the system.

When one company is viewing the profile of another company in my site I also need to show whether that representative of the company whose profile is being viewed in logged in the system at that time or not so that if the user wants then can send a message to them.

I need some advice on how can I achieve this in the best possible way, where I can display the total number the total number of correct logged in members

Thanks a lot!

Cheers
Paresh

d11wtq | What was with the double line-spacing? :?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Bad design without some sort of socket checking to *ping* the user or such like...

Better idea:

Don't store "1", store the date/time of the last action they performed... every page change, every link clicked etc etc triggers that field to update in the database.

Then all you need to do is check total number of user last active in the past 20 mins for example. Works a treat ;)
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

Post by phpScott »

most are done with a timestamp that gets update everytime a logged in user visits a page.

Then when you want to see how many users are logged in you do a check to see if the timestamp is more current then say now - 10 minutes.

you can also follow the line in the tutorials section
viewtopic.php?t=29342

which will help
Post Reply