问题
I am trying to force log out an inactive user from my CMS. The timeout value is stored in a value called PREF_TIMEOUT in my database. Ive borrowed this code and modified it a little. The code does not seem to be doing anything. Does anyone know of a better method of doing this or can spot what is breaking it?
<?php
function init() {
parent:: init();
self::logoutInactiveUser();
}
$timeout = mysql_query("SELECT PREF_TIMEOUT FROM preferences WHERE PREF_ID = '1'");
$result = mysql_fetch_array($timeout);
function logoutInactiveUser() {
$inactivityLimit = $timeout * 60; // Converted to seconds
$sessionStart = Session::get('session_start_time');
if (isset($sessionStart)){
$elapsed_time = time() - Session::get('session_start_time');
if ($elapsed_time >= $inactivityLimit) {
$member = Member::currentUser();
if($member) $member->logOut();
Session::clear_all();
Director::redirect(Director::baseURL() . 'Security/login');
}
}
Session::set('session_start_time', time());
}
?>
回答1:
Refer to my answer at the link: Logout an inactive user using PHP .
As per your current code, the issue is your Queries
$timeout = mysql_query("SELECT PREF_TIMEOUT FROM preferences WHERE PREF_ID = '1'");
$result = mysql_fetch_array($timeout);
are outside the function logoutInactiveUser(), due to which the variable $timeout will not have any data from your database. Moving the code inside the function should help you.
来源:https://stackoverflow.com/questions/30526802/how-to-automatically-logout-an-inactive-user-in-your-cms-php