Page 1 of 1

how do I update database after user leaves my site?

Posted: Tue Apr 15, 2008 5:01 pm
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!

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

Posted: Tue Apr 15, 2008 5:05 pm
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.

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

Posted: Tue Apr 15, 2008 6:30 pm
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?

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

Posted: Tue Apr 15, 2008 7:53 pm
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.

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

Posted: Wed Apr 16, 2008 5:47 am
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.

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

Posted: Wed Apr 16, 2008 5:55 am
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.

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

Posted: Wed Apr 16, 2008 5:57 am
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.