Jump to content


Photo

Sessions: Detect online status of members? Ideas?


  • Please log in to reply
5 replies to this topic

#1 cronoklee

cronoklee
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 09 June 2006 - 01:08 AM

I have a simple membership system on my site using PHP & mySQL. I track users with sessions using session_start() at the top of every page.
I was wondering if anyone knows how I could detect members online status? I'd like to have a note beside each username, saying if they are online now or not.

Thanks a lot,
Ciaran

#2 akitchin

akitchin
  • Staff Alumni
  • Advanced Member
  • 2,516 posts
  • LocationCalgary, AB, Canada

Posted 09 June 2006 - 01:25 AM

one way that i used to do this is by updating a database with the last action of each user (essentially the last time the user requested a page). on every page i updated a table with the user's id, and the current datetime. when echoing how many users were online (or, in your case, whether or not a user is online) i simply grabbed the users that had requested a page in the last 5 minutes.

a few notes about this. first of all, if you've got a lot of users likely to be on at the same time, as well as a lot of page hits, i'm pretty sure you're going to choke the hell out of your database. second, someone else may have a better idea for this - this is just a home-brewed method. i never knew whether the server itself had a list of current session connections or cookies. if it does, you may want to consult that instead, as it gives you a direct link to who is and isn't currently online.

#3 Fyorl

Fyorl
  • Members
  • PipPipPip
  • Advanced Member
  • 273 posts
  • LocationUK

Posted 09 June 2006 - 01:35 AM

I would agree with akitchin's method. If you wanted to reduce the load on your database you could have it updated once they log in and keep a session variable of it. Then each page could check whether there was a time difference greater than 5 minutes between the current time and the session time and if so, update the session and the database again, then in your script to check if people are logged in, it would pull every value from the database and check if there was a time difference greater than 5 minutes and if so, do not show the user as online. This isn't as accurate but you could adjust the accuracy by fiddling with the times
[table]



Don't worry, the printer fairies will sort it out.

#4 akitchin

akitchin
  • Staff Alumni
  • Advanced Member
  • 2,516 posts
  • LocationCalgary, AB, Canada

Posted 09 June 2006 - 01:53 AM

dynamite solution to the database lagging. thanks Fyorl.

#5 Fyorl

Fyorl
  • Members
  • PipPipPip
  • Advanced Member
  • 273 posts
  • LocationUK

Posted 09 June 2006 - 01:59 AM

[!--quoteo(post=381686:date=Jun 8 2006, 08:53 PM:name=akitchin)--][div class=\'quotetop\']QUOTE(akitchin @ Jun 8 2006, 08:53 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
dynamite solution to the database lagging. thanks Fyorl.
[/quote]

^_^
[table]



Don't worry, the printer fairies will sort it out.

#6 cronoklee

cronoklee
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 14 June 2006 - 08:13 PM

Great - I'll try it out. Thanks a lot ;)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users