Here is my take on the cron to update the online table or users table when users leave without logoff or are online for awhile. I figure the cron should probably run 2x per hour
Have a look and see if you can think of anything i missed, something that might cause a loop or stall, i think i covered the bases.
All names have been changed for security.
So we have the users table and the online table in the comparrison
//get all users in the online table
$u_table = $db->onlineUsers(); //everyone in the online table
if(is_array($u_table) && !empty($u_table))
{
foreach($u_table as $ukey => $uval)
{
//u_ut is user table
$u_ut = $db->LastLogin( (int) $uval['id']); //last login info from users table
$userid = (int) $uval['id']; //user id
//users table info
$logindate = clean($ud_ut['login_date']);
$loginstamp = clean($ud_ut['login_stamp']);
$ip = clean($ud_ut['ip']); //the ip is only for passing data to update the record - or code a new function
//online table info
$ol_stamp = clean($uval['stamp']);
$ol_date = clean($uval['date']);
//if users table data GT online tables data
//user is still online - update online table
if( (int) $loginstamp > (int) $ol_stamp )
{
//update the online table
$data = [
'stamp' => $loginstamp,
'date' => $logindate
];
$update_online_table = $db->UserLogTable( (int) $userid, $data);
}else{
//user has left without logging off
//update the users table
$data = [
'login_stamp' => $ol_stamp,
'login_date' => $ol_date,
'ip' => $ip
];
$update_users_table = $db->rUpdateUsersTable( (int) $userid, $data);
//remove user from online table
//they are no longer here so remove them from the online table
$remove = $db->removeFromOnlineTable( (int) $userid);
//this should not invoke unless there is a problem
//because it only has acess if there is a record
//so if it fails then something is actually wrong
if(!$remove)
{
error_log("Update Login Table Cron Error", 0);
}
}//close else
}//close foreach
//echo "finished"; for testing
exit;
}//close if array utable