I\'ve created a very simple multiuser game for learning purposes.
As users log on, each other user gets an update of all currently logged in users.
When a us
I assume that, users are shown as online and offline on logout. Destroying session or cookie will require client browser in working mode.
Solution
I also assume there is a timestamp column maintained. Set an interval to check time-gap between current TS and Last Timestamp. Also update your own TS against your id to current TS.
setInterval(function(){
$.ajax({
url: "/backend.php?userid=id",
success: function(resoponse){
console.log(response);
}
});
}, 10000);
backend.php
$user_id = $_GET["userid"];
$query = "select * from table name where (GETDATE() - timestamp) > 60";
$resut = mysqli_query($con, $query);
while($row = mysqli_fetch_assoc($result)){
//set there status = 0 as offline
$other_user_id = $row['user_id'];
mysqli_query($con, "update tablename set status = 0 where user_id = '$other_user_id'");
}
//update your own timestamp
mysqli_query($con, "update tablename set timestamp = GETDATE() where user_id='$user_id'");
This would check user activity, or basically check if that js is running or not. If browser was closed then TS for that user won't get updated hence GETDATE() - timestamp will be greater than 60. Now other users who would be running the web app would also run the same script, checking and updating all users status as per condition. When user closes his tab he would still be online for at least 1 minute.
I had a similar problem and found solution here PHP auto logout on tab close.