如何使用codeigniter自动注销用户在特定时间内是否处于非活动状态

时间:2013-04-22 14:18:14

标签: php codeigniter logout

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

3 个答案:

答案 0 :(得分:6)

// 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()"

答案 1 :(得分:1)

您可以节省用户在sessioncookie

中登录的时间

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

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

答案 2 :(得分: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)。< / p>