Hi,
I'm working on a site and not only do I want to control access, I also want to be able to check who might be using the system at any one time. To do this each time someone logs in I register them in a MySQL active_users table. This table has a lastaction column which logs the time of each action.
The following function is supposed to log them out if they leave the system idle for a set period of time, but it just won't work. It seems to be returning the values correctly, but it keeps timing users out as soon as they log in.
Please does anyone have any ideas, I'm pulling my hair out here!!!
function check_timeout()
{
global $valid_user;
// set maximum idle time to 1/2 hour = 1800
define("MAX_IDLE", 1800);
// set the timeout value to 1/2 hour ago
$timeout = date("Y-m-d H:i:s", time("Y-m-d H:i:s")-MAX_IDLE);
echo "<br/>timeout value = $timeout<br/>";
$chk_result = mysql_query ("SELECT * FROM active_users WHERE username='$valid_user'");
if ($chk_result)
{
$row = mysql_fetch_object($chk_result);
$lastaction = $row->lastaction;
echo "<br/>lastaction value = $lastaction<br/>";
//where sessions have timed out., delete user from active_users
//and log them out
$del_query = ("DELETE FROM active_users WHERE username='$valid_user' AND lastaction <'$timeout'");
$del_result = mysql_query ($del_query);
if ($del_result)
{
echo "<br/>Session Timed Out!<br/>";
echo('<META Http-equiv="refresh" Content="1; Url=logout.php">');
//exit;
}
else
{
update_last_action();
}
}
}