views:

93

answers:

7

Hello.
I have a login system. With my login session stores and cookies (if you "remember me") and then you login. When you're inside the login system at home.php (you can only enter if session/cookie is registered), i want the status field in "user" table, to update to online and offline when you sign out.

I know how to do this, to update the status when you login and log out, but here's the thing: How do i do when the user leaves the site without logging out (pressing on the actual log out button) ? I mean if the status is set to Online and you leave the page, it will remain Online you may understand..logic.. so what do i do at this situation? I don't want to have a little ugly popup that log you off when you leave the page, that's so old school and bad.

Or maybe when the session runs out then you set the status to offline? but again, how should i do that?

Ideas and examples on solution for this would be good.

Sites like e.g facebook, in the Chat you change status to Idle if you leave the pages, and somehow if you leave the page and there goes some time you are offline... How, I don't know.

+3  A: 

Facebook is a bit nasty in that their page contains some Javascript that keeps an open connection to their chat server. Once the connection is lost, it means you've closed the page (or your internet connection) and you're marked offline.

Usually, just a timeout is used that marks the user offline some time after their last activity/page load. A reasonable value for this timeout could for instance be the time after which their session cookie expires.

Wim
+1 Nice tactic, I hadn't thought of that.
Artefacto
@Wim hello, I dont quite understand your sessiontimeout method, how should it run a query AFTER the user have leaved the page and not clicked on something? Can the server run a query itself with session-timeout or? Could you please also provide a little example
Karem
Your database just contains the time of the last visit, not an actual online/offline field. Once you want to know the online/offline status (during another visitor's page load for instance), get the last access time from the database and subtract it from the current time, then compare with your timeout.
Wim
You could also run a periodic task that marks users as offline, but the overhead of that is probably much worse than doing the comparison each time you want to know the status.
Wim
Oh okay ok i think i got it, but i dont know how to "subtract it from the current time and compare with your timeout", LastTime-TimeNow = timeout, in coding, could you make a example of that, please thank you.
Karem
If `$last_access` is the time you got from the database, and `$timeout` the timeout in seconds, the user would be offline if `time() - $last_access > $timeout`.
Wim
Thank you, when i store $last_access on each page, should it just stoers like this (unix format) $last_acess = time(); ? thank you last question before im trying it out..
Karem
Yes that's the easiest way, just use `time()` and an INTEGER field in MySQL. You could use DATETIME or TIMESTAMP fields, but then it gets more complex.
Wim
Karem
A: 

How do i do when the user leaves the site without logging out (pressing on the actual log out button) ?

Well, you can't detect that. The best you can do is a "last active" statistic or "users active in the last then minutes". In this case, you should update a database field each type the user makes a page request.

Artefacto
Yes you can (for the most part). [See this](http://ajaxpatterns.org/Heartbeat)
NullUserException
@Null I see no contradiction. That's exactly what I said, except the age requests are forced on an interval.
Artefacto
A: 

You can store a timestamp on pageload, then base online/offline status on the amount of time that has passed since.

Zurahn
A: 

You could have in the user's table a field called last_visit for example, and update it with the sysdate, now(), etc when he accesses any page...

In the query that gives you the online users, you will filter it with the users that have in the field last_visit ten minutes or less....

like:

where last_visit < today-(10 minutes)
Garis Suero
+1  A: 

The simple solution is to save the time of the last access of a user and consider him as offline if he really logged-out or if that time is too long in the past.

For a better solution (similar to facebook) you need to use semi-persistent connnections and use them to detect presence with more granularity.

gustavogb
A: 

I guess you should do something similar to AJAX push. @Wim suggests a similar approach as well.

thelost
FYI @username only works in comments, not answers
Wim
@Wim Thanks, is there a way to use them within answers as well ?
thelost
Not that I know of. You could post a comment in the other guy's answer though and in there refer to your own.
Wim
A: 

I've been writing a site in php as a hobby recently, change from the norm and my take on this was, where sessions are being stored in files to:

  • Have a separate table and list user id, last_update time, session id
  • store a variable in the session called last_active
  • On each page call check if the last active time was less than 30 seconds (or whatever you want) if it's longer then update the table in step one, if not carry on. This stops you needing a call on every page and will be accurate to within a minute or so.

The session id in the database is unnecessary, just nice to see who's got what session id active, I even wrote a bit of a hackish function to delete the session files based on this id on a manual basis, if I wanted to log someone out... no real reason for it, just a bit of fun.

Of course if you're saving sessions in your database then it's even easier as you're doing a database trip every page anyway so updating it isn't that much of an issue.

You can also add some ajax to keep the db updated even when the page is open but not being actively refreshed then as someone else pointed out you check who's online by checking out whose last update database entry is within whatever arbitrary range you want.