首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用户在线PHP

用户在线PHP
EN

Stack Overflow用户
提问于 2014-02-09 06:30:06
回答 1查看 853关注 0票数 1

我有一个登录类:

代码语言:javascript
复制
    while($stmt->fetch()) {
        $_SESSION['some_session'] = $username;
        $_SESSION['time_session'] = time();
        header("Location: home.php");
        exit();
    }

还有我的user_online脚本:

代码语言:javascript
复制
public function check_if_logged_in() {
    if(time() - $_SESSION['time_session'] > 3600) {
        session_start();
        $users->logout($_SESSION['some_session']);

        session_start();
        session_destroy();

        header("Location: index.php?e=expired");
        exit();
    }
}

还有我的注销课:

代码语言:javascript
复制
public function logout($username) {
    $j = 0;
    $stmt = $this->mysqli->prepare("UPDATE blog_users SET is_online=? WHERE username=?");
    $stmt->bind_param('ss', $j, $username);
    $stmt->execute();
    $stmt->close();
}

我的问题是:当用户试图导航时,我的系统会检查用户是否经过了3600次联机,如果他/她已经过了,就会将其注销。我想要发生的事情:如果没有用户导航,如果用户经过了3600的联机时间,它应该自动将他们的is_online状态更新为数据库中的0,这有可能吗?您可能想知道为什么我要做is_online表,因为我想在网上向用户展示。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-02-09 06:38:29

如果您希望用户自动退出,如果他/她不执行任何操作,则必须使用javascript检查或使用ajax,并将每个操作存储在某个地方。

javascrip解决方案

像这样的东西应该能起作用

代码语言:javascript
复制
jQuery(document).ready(function() {
  countDown();
  $('body')
          .change(function() {//reset the counter if change any value in 
                              //the page http://api.jquery.com/change/
    resetCounter();
  })
          .mousemove(function() {//reset the counter if the mouse is moved inside the body element
                                 //http://api.jquery.com/mousemove/
    resetCounter()
  })
          .click(function() {//reset the counter if the click inside the body element
                             //http://api.jquery.com/click/
    resetCounter()
  });
  $(window).scroll(function() {//reset the counter if make scroll
                              //http://api.jquery.com/scroll/
    resetCounter()
  });
});
var seconds = 3600; //set the var seconds to the time you want start to check
function countDown() {
  if (seconds <= 0) {//when the time over execute
    window.location = "logout.php";
  }
  seconds--;//run every second to decrees a second
  window.setTimeout("countDown()", 1000);

}
function resetCounter() {//reset counter
  seconds = 3600;
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21656150

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档