how do I update database after user leaves my site?

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
Yourchild
Forum Newbie
Posts: 16
Joined: Mon Jul 30, 2007 3:04 pm

how do I update database after user leaves my site?

Post by Yourchild »

I am so stuck on this topic. I've been stuck for days now and this is starting to really take the toll...

I have a site that allows users to log in....

When the user clicks the 'log off' button, I can easily go to the database and update it, setting the field 'is_logged_in' to false...

But when if the user closes the browser, or navigates to another site, how am I to go into the database and perform the same update?

As admin of the site, I want to be able to get a list of currenly logged in users. But I dont want to implement the timestamp method. I need to know the exact time when the user logged out.

Please help!
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: how do I update database after user leaves my site?

Post by Chris Corbyn »

Not possible to do reliably. The best thing to do is to track the last time they click any link on your site and base your "users online" as "users active in past 5 minutes" or something. Checking only for logged in users doesn't give an accurate idea of the number of users online neither since PHP sessions can last for a long time even if the user is idle.
User avatar
seodevhead
Forum Regular
Posts: 705
Joined: Sat Oct 08, 2005 8:18 pm
Location: Windermere, FL

Re: how do I update database after user leaves my site?

Post by seodevhead »

Not to hijack or anything... but how does the whole "Who's Online" feature work? Everytime there is a page accessed in a forum, for instance, there is data written to the database as far as user_id, page location and time? In other words, every page a logged in user accesses creates a database write?
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: how do I update database after user leaves my site?

Post by califdon »

Yourchild wrote:When the user clicks the 'log off' button, I can easily go to the database and update it, setting the field 'is_logged_in' to false...

But when if the user closes the browser, or navigates to another site, how am I to go into the database and perform the same update?
As Chris said, an exact "log out" can only be recognized by a user explicitly clicking a button. Remember, HTTP is a "stateless" protocol. Once the server sends data to a client, there is no inherent connection maintained--it simply has no way of knowing whether the client is still displaying the data unless the client sends another request.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: how do I update database after user leaves my site?

Post by Chris Corbyn »

seodevhead wrote:Not to hijack or anything... but how does the whole "Who's Online" feature work? Everytime there is a page accessed in a forum, for instance, there is data written to the database as far as user_id, page location and time? In other words, every page a logged in user accesses creates a database write?
Yes. I I'm not a fan of performing writes on GET requests but that's basically what this forum does, along with most other implementations.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: how do I update database after user leaves my site?

Post by onion2k »

If you're logging to your database anyway, as a forum does in order to track what you've read, it's a pretty easy feature to add and it doesn't really cost anything besides a select. It's probably not worth writing a logging system specifically for that feature though.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: how do I update database after user leaves my site?

Post by Chris Corbyn »

onion2k wrote:If you're logging to your database anyway, as a forum does in order to track what you've read, it's a pretty easy feature to add and it doesn't really cost anything besides a select. It's probably not worth writing a logging system specifically for that feature though.
Yeah, that's true. On a forum you've got writes happening on all page requests anyway so it's a non-issue.
Post Reply