0

How do i create a script that will log out the user after 10 minutes of inactivity.

Thanks.

AAA
  • 3,120
  • 11
  • 53
  • 71

3 Answers3

2

For PHP, include this code in commonfile

if(isset($_SESSION['last_activetime'])){    
   if(time() - $_SESSION['last_activetime'] > 600) { 
        header("location:logout.php");
        exit;
   }    
}
$_SESSION['last_activetime'] = time(); // when user open page time store in session
Adi
  • 5,089
  • 6
  • 33
  • 47
Apoorva Shah
  • 632
  • 1
  • 6
  • 15
1

PHP - make session expire after X minutes :)

Community
  • 1
  • 1
Spyros
  • 46,820
  • 25
  • 86
  • 129
1

Each time the user does something, update a timestamp in a table somewhere for that user. Have a cron job on the server that looks in that table, and logs out anyone that is logged in but haven't had his timestamp updated in ten minutes.

Stefan H Singer
  • 5,469
  • 2
  • 25
  • 26
  • OK. I get the idea of how to do it. But how can i integrate it into my code: Here it is: if(!isset($_SESSION['user_name']) && $_SERVER['PHP_SELF'] != '/log.php') { header("Location: http://$_SERVER[HTTP_HOST]"); } – AAA Mar 20 '11 at 22:38
  • 1
    Hm, totally depends on exactly what you want to do. I meant that you could store it in a database table. But you should be able to do something like if(isset($_SESSION['lastActivityTimestamp'] && $_SESSION['lastActivityTimestamp'] < ten minutes ago) logout(); – Stefan H Singer Mar 22 '11 at 12:28
  • OK. I am going to be trying it soon. I will get back to you if i have any problems. thanks Stefan. – AAA Mar 22 '11 at 21:00