1

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

4

3 回答 3

9
// 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()"
于 2013-04-23T09:31:18.937 回答
1

您可以节省用户登录 asession或 a 的时间cookie

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

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

于 2013-04-22T15:41:29.143 回答
0
<?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)。

于 2014-05-28T05:57:26.933 回答