Advertisement
luistavares

PHP - Expirando a sessão por tempo

Aug 4th, 2018
364
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.82 KB | None | 0 0
  1. <?php
  2.  
  3. //Start our session.
  4. session_start();
  5.  
  6. //Expire the session if user is inactive for 20 minutes or more.
  7. $expireAfter = 20;
  8.  
  9. //Check to see if our "last action" session variable has been set.
  10. if(isset($_SESSION['last_action'])){
  11.    
  12.     //Figure out how many seconds have passed since the user was last active.
  13.     $secondsInactive = time() - $_SESSION['last_action'];
  14.    
  15.     //Convert our minutes into seconds.
  16.     $expireAfterSeconds = $expireAfter * 60;
  17.    
  18.     //Check to see if they have been inactive for too long.
  19.     if($secondsInactive >= $expireAfterSeconds){
  20.         //User has been inactive for too long. Kill their session.
  21.         session_unset();
  22.         session_destroy(); 
  23.     }    
  24. }
  25.  
  26. //Assign the current timestamp as the user's latest activity
  27. $_SESSION['last_action'] = time();
  28.  
  29. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement