如果用户在特定时间内处于非活动状态,如何使用编码器自动注销


how to auto log out if user is inactive for some specific duration using codeigniter

如果用户在一段特定的时间内处于非活动状态,那么它应该自动注销。那么我如何使用codeigniter做到这一点呢?或如何检查用户登录该网站后是否活跃?

// Add the following into your HEAD section
var timer = 0;
function set_interval() {
  // the interval 'timer' is set as soon as the page loads
  timer = setInterval("auto_logout()", 10000);
  // the figure '10000' above indicates how many milliseconds the timer be set to.
  // Eg: to set it to 5 mins, calculate 5min = 5x60 = 300 sec = 300,000 millisec.
  // So set it to 300000
}
function reset_interval() {
  //resets the timer. The timer is reset on each of the below events:
  // 1. mousemove   2. mouseclick   3. key press 4. scroliing
  //first step: clear the existing timer
  if (timer != 0) {
    clearInterval(timer);
    timer = 0;
    // second step: implement the timer again
    timer = setInterval("auto_logout()", 10000);
    // completed the reset of the timer
  }
}
function auto_logout() {
  // this function will redirect the user to the logout script
  window.location = "your_logout_script.php";
}
// Add the following attributes into your BODY tag
onload="set_interval()"
onmousemove="reset_interval()"
onclick="reset_interval()"
onkeypress="reset_interval()"
onscroll="reset_interval()"

您可以节省用户在sessioncookie中登录的时间

例子:$this->session->set_userdata('time', time());并使用javascriptjQuery函数(Exp. $.getJSON('time.php', function (data) {alert(data.serverTime);});)或其他任何检查当前时间。然后,在需要时注销用户。

但是,下次请放置代码或其他东西来显示您的努力。

<?php 
    $minutes=3;//Set logout time in minutes    
    if (!isset($_SESSION['time'])) {
        $_SESSION['time'] = time();
    } else if (time() – $_SESSION['time'] > $minutes*60) {
        session_destroy();
        header(‘location:login.php’);//redirect user to a login page or any page to which we want to redirect.
    }
?>

…它最初取自skillrow.com/log-out-user-if-user-is-inactive-for-certain-time-php/(现在是404)。