简体   繁体   中英

How to logout user after 30 mins in php when there is no activity?

I am working on a website in which I want to logout user after 30 mins when there is no user activity.

The php code code which I have used in order to start a session is:

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


Problem Statement:

I am wondering what changes I need to make in the php code above so that it logout user after 30 mins when there is no activity.

I have added the following code in the code above but it doesn't seem to work.

 /* 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)  */  

I don't know why you still don't get it from the comments. Anyways this is what they all meant. Do this.

 /* 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)  */  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM