php, updating database when users appear inactive

Associate
Joined
11 Oct 2008
Posts
268
When a user logs in to my members area, their online status is set to 2 in my database and they are displayed as online for everyone else to see. And when they click log out their online status is set to 1 and they are no longer shown as online.

My problem is when the session expires due to inactivity, the database isnt updated and they are still shown as online. So whenever the access a page in the members area I have made it so that time also gets entered into the database. And my goal is to have the users online page auto refresh every 5 minutes to check the current time against the time stored in the database for that user and if 15 minutes has past, have the online status in the database updated to 1 again.

im using pdo but im a novice at it and havnt been able to find much help on google. Is there a way i can use an if statement to check the two times and only update the online field to 1 for the inactive users while leaving the users who appear active as 2?

Sorry if this was confusing, i wasnt too sure how to word it properly.
 
You essentially want to store the users last activity time which is updated on every page load/refresh, then CRON/Schedule to run (ever 5/10mins etc) through the users last activity time updating (eg - "UPDATE `users` SET `online_status` = 1 WHERE `last_activity` < DATE_SUB(now(), INTERVAL 15 MINUTE)" etc) their online status value where last activity is > 15 mins (generally a few more mins that session expire time).
 
Last edited:
Depends how up to date you need to be with the info. Phunky's suggestion is a good one if you don't need too regular updating but if you need to be very reactive you would probably need to use some ajax to keep posting home to let you know they are still there.

Cron job will work to update but if someone is on the same page for a long time it will tell you they are logged out when they are just lingering in one place too long.
 
Depends how up to date you need to be with the info. Phunky's suggestion is a good one if you don't need too regular updating but if you need to be very reactive you would probably need to use some ajax to keep posting home to let you know they are still there.

Cron job will work to update but if someone is on the same page for a long time it will tell you they are logged out when they are just lingering in one place too long.

The session would expire anyway and if you were continuously auto-updating/refreshing then the last activity timestamp (and session) would be getting updated. Their online status would only get updated after X mins when they close the page.

You could also employ the JQuery Unload event to update via AJax, although granted it doesn't work on all browsers and it will fire when navigating away from (links/form submits) or refreshing the page ( however you could exclude links etc from firing the event).

Edit - Should have read Phunky's reply, doh!
 
Back
Top Bottom