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?