1

I have some query regarding browser close issue.

I am using xcart.When user have logged in website and Any situation browser will be closed any so session will be destroyed..and also run the sql query for user status will be offline.

Regards, Manish Patel

Manish
  • 191
  • 10
  • possible duplicate of [How to automatically log out user when browser closes?](http://stackoverflow.com/questions/10116292/how-to-automatically-log-out-user-when-browser-closes) – Quentin Jul 16 '12 at 09:08

1 Answers1

1

The code to update the users status could be moved into an include file, something along the lines of this:

$time = date('Y-m-d H:i:s', strtotime('now -15 minutes'));
$sql = "UPDATE users SET status = 'offline' WHERE last_activity < '{$time}'";

This could then be included on the site home page for example, and is independant of who is actually online.

You would obviously need a column (timestamp would be nice here) to record their activity

Dale
  • 10,384
  • 21
  • 34
  • This would only work if he has a steady stream of traffic. Even if has too much it's going to be excessive writes on the database. Much more reliable to use a [`crontab`](http://linux.die.net/man/5/crontab). – Martin Jul 16 '12 at 09:12
  • It wouldn't matter how much traffic he has, the next time one person visits it's going to update the database and set everyone offline. It may be better to go with crontab if it is a heavy load site, but if it isn't then the cron job itself is causing extra overheads. Always different needs and never one way fits all. – Dale Jul 16 '12 at 09:15
  • You're assuming that the only entity reading the database is the website, and i'm pretty sure one query every few minutes isn't considered 'extra overheads' in anyones books ;) – Martin Jul 16 '12 at 09:17
  • I'm just offering an answer feel free to post your own – Dale Jul 16 '12 at 09:26