0

I have a website I'm creating and I want to be able to see who is online and who is not. Seeing who is online is not hard, when you log in, "online" field in database changes from 0 to 1. But the problem is with going offline. I managed to do that IF the person logs out on the log out button and gets redirected to logout.php page:

<?php
session_start();
$user_check=$_SESSION['login_user'];

if(session_destroy())
{
$connection = mysql_connect("localhost", "root", "");
$db = mysql_select_db("masterrad", $connection);

$query2 = mysql_query("update users set online = '0' where username = '$user_check'", $connection);

header("Location: index.php");
}
?>

But if he just closes the page nothing will change in the database since he never went to the logout.php page.

So I was wondering if there is a better way to do this?

zmuci
  • 488
  • 5
  • 20
  • 1
    Sessions will/should automatically delete themselves, depending on how you've setup your server's session life. The updating, well, that's another ball of twine. Google "update database if browser is closed". You will find many results. – Funk Forty Niner Jan 20 '15 at 22:02
  • This is an awful idea, but you can have a "heartbeat" system where you send an ajax request every x minutes. If you don't receive an answer, mark as offline. – Ismael Miguel Jan 20 '15 at 22:03
  • Yes, a "heartbeat" __is__ an awful idea. – Kleskowy Jan 20 '15 at 22:06
  • For instance, this answer http://stackoverflow.com/a/1658474/ may very well answer your question and https://www.zulius.com/how-to/close-browser-connection-continue-execution/ – Funk Forty Niner Jan 20 '15 at 22:09
  • Out of curiosity, why is a heartbeat system being so vehemently denied here? I've encountered a number of production sites that use that paradigm. – Alec Deitloff Jan 20 '15 at 22:21
  • I made it work with javascript and its working great, but i quess that should not be the final work, since users can disable java etc. Ill try to figure out that ignore_user_abort function. Thank you. – zmuci Jan 20 '15 at 22:22

2 Answers2

2

On each page that you consider to be an 'online' page.
You could use @Alec Deitloffs idea of a 'last_time_spotted' field and update the online flag based on that and your time period. No cron needed.

$query2 = mysql_query("update users set `online` = '0' where `online` = '1' AND (`last_time_spotted` < NOW() - INTERVAL 15 MINUTE), $connection);
greg_diesel
  • 2,770
  • 1
  • 13
  • 23
0

The way that most sites perform this, I believe, is by having a "last time spotted" field in the database. Every time the user goes to a new page, that field is updated for them to the current time. A cron job would execute every 10, 15 minutes (whatever you want), and if the current time minus the time the user was last spotted is greater than the threshold you want (ie, inactive for seven minutes), then you would consider them offline.

It's very, very difficult to actually have code execute completely and successfully when a user leaves the page, closes the browser, et cetera. Unfortunately, this means that alternative methods need to be taken, and it's my understanding that this is the most popular choice. Or at least, one of the simpler ones to implement.

Funk Forty Niner
  • 73,764
  • 15
  • 63
  • 131
Alec Deitloff
  • 457
  • 4
  • 12
  • I am using windows, and for what I've saw cron is for linux? Or did i missed the point totally? – zmuci Jan 20 '15 at 22:20
  • [This question](http://stackoverflow.com/questions/132971/what-is-the-windows-version-of-cron) discusses alternatives for establishing timed/repeated operations. Basically, you would just want to establish something on the server to run every X units of time that would perform the database sweep for "inactive users" and convert them into "departed users." – Alec Deitloff Jan 20 '15 at 22:23