【问题标题】:how to auto log out if user is inactive for some specific duration using codeigniter如果用户在特定时间内不活动,如何使用 codeigniter 自动注销
【发布时间】:2013-04-15 11:50:18
【问题描述】:

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

【问题讨论】:

  • 问题显示没有研究工作。请展示你的作品。
  • 阅读 CI 文档以供阅读.. :) ellislab.com/codeigniter/user-guide/libraries/sessions.html $this->session->sess_destroy();
  • 注意不要在用户可能需要一段时间来输入数据的数据输入页面中实现,例如30 分钟(在我的情况下)

标签: php codeigniter logout


【解决方案1】:
<?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)。

【讨论】:

  • @Matt:根据您的代码,用户将在 x 分钟后注销,在这种情况下为 3 分钟,即使用户处于活动状态。这是我修改后的解决方案: $minutes = 3; if(isset($_SESSION['time']) && (time()-$_SESSION['time'] > $minutes*60)){ header('location:'. 'logout_url.php');死(); } // 如果为空且用户已登录,则设置时间 if($userLoggedIn){ $_SESSION['time'] = time(); }
  • @shairya:这不是我的代码,它是 kamal 的。我只有edited his answer
【解决方案2】:
// 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()"

【讨论】:

【解决方案3】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-09-28
    • 2013-09-23
    • 2010-10-09
    • 1970-01-01
    • 2012-12-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多