How to automatically logout an inactive user in your CMS PHP

浪尽此生 提交于 2019-12-25 03:44:28

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!