簡體   English   中英

用戶在線PHP

[英]Users Online PHP

我有這個登錄課程:

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

和我的user_online腳本:

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();
    }
}

還有我的注銷類:

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表,這是因為我想在線顯示用戶。

如果您希望用戶不執行任何操作而自動注銷,則必須使用javascript或ajax進行檢查,並將每個操作存儲在某個位置。

javascrip解決方案

這樣的事情應該工作

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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM