没有活动时,如何在php中30分钟后注销用户?

时间:2019-03-05 16:20:27

标签: php session cookies logout user-inactivity

我正在一个网站上,要在30分钟内没有用户活动时注销用户。

我用来启动会话的php代码是:

<?PHP
   session_start();

 /* Code Added (START)  */ 

   if (isset($_SESSION['LAST_ACTIVITY']) && (time() - $_SESSION['LAST_ACTIVITY'] > 1800)) {
      // last request was more than 30 minutes ago
      session_unset();     // unset $_SESSION variable for the run-time
      session_destroy();   // destroy session data in storage
   }
   $_SESSION['LAST_ACTIVITY'] = time(); // update last activity time stamp

  /* Code Added (END)  */    


      // Logout
      if(isset($_GET['user_logout'])){
          unset($_SESSION['pageadmin']);
          header('location: /emailers/landing_admin.php');
          exit();
      }


      if (!empty($_POST) && isset($_POST['user_login']) && $_POST['user_login']==1 && !isset($_SESSION['pageadmin'])){
          $user_arr = array(
              'page'=>'hello',
          );
          if(array_key_exists($_POST['user_name'], $user_arr) && $user_arr[$_POST['user_name']] === $_POST['user_pass']){
              $_SESSION['pageadmin'] = true;
              $_SESSION['pageadmin_user'] = $_POST['user_name'];
          }else{
              $_SESSION['msg'] = 'Invalid user name or password';
          }
      }

// Is user logged in?

    if(!isset($_SESSION['pageadmin'])){
        ?>
        <form action="/emailers/landing_admin.php" method="post">
            <div style='width:400px;'>
                <input type="hidden" id="user_login" name="user_login" value="1">
                <fieldset>
                    <legend>Login</legend>
                    <?php if(isset($_SESSION['msg'])){echo '<div style="color:red;">'.$_SESSION['msg'].'</div>';unset($_SESSION['msg']);}?>
                    <div>
                        <label for="user_name">User Name</label>
                        <input type="text" name="user_name">
                    </div>
                    <div>
                        <label for="user_pass">Password</label>
                        <input type="password" name="user_pass">
                    </div>
                    <div>
                        <button type="submit">Login</button>
                    </div>
                </fieldset>
            </div>
        </form>
        <?PHP
    }


问题陈述:

我想知道我需要在上面的php代码中进行哪些更改,以便在30分钟没有活动时注销用户。

我在上面的代码中添加了以下代码,但似乎无法正常工作。

 /* Code Added (START)  */ 

   if (isset($_SESSION['LAST_ACTIVITY']) && (time() - $_SESSION['LAST_ACTIVITY'] > 1800)) {
      // last request was more than 30 minutes ago
      session_unset();     // unset $_SESSION variable for the run-time
      session_destroy();   // destroy session data in storage
   }
   $_SESSION['LAST_ACTIVITY'] = time(); // update last activity time stamp

 /* Code Added (END)  */  

1 个答案:

答案 0 :(得分:1)

我不知道为什么您仍然无法从评论中得到它。无论如何,这就是他们的全部意思。 做这个。

 /* Code Added (START)  */ 

   if (isset($_SESSION['LAST_ACTIVITY']) && (time() - $_SESSION['LAST_ACTIVITY'] > 1800)) {
      // last request was more than 30 minutes ago
      //session_unset();     // unset $_SESSION variable for the run-time
      session_destroy();   // destroy session data in storage
header('location:index.php'); // redirect to login page or home page
   }
   $_SESSION['LAST_ACTIVITY'] = time(); // update last activity time stamp

 /* Code Added (END)  */