如何在CMS PHP中自动注销不活动的用户 [英] How to automatically logout an inactive user in your CMS PHP
问题描述
我正在尝试从CMS中注销不活动的用户.超时值存储在数据库中名为PREF_TIMEOUT的值中.香港专业教育学院借用此代码,并对其进行了一些修改.该代码似乎没有做任何事情.有谁知道这样做的更好方法,还是可以发现破坏它的原因?
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());
}
?>
推荐答案
Refer to my answer at the link: Logout an inactive user using PHP .
根据您当前的代码,问题在于您的查询
$timeout = mysql_query("SELECT PREF_TIMEOUT FROM preferences WHERE PREF_ID = '1'");
$result = mysql_fetch_array($timeout);
在函数 logoutInactiveUser()之外,因此变量 $ timeout 不会包含数据库中的任何数据.在函数内部移动代码应该会对您有所帮助.
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.
这篇关于如何在CMS PHP中自动注销不活动的用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!