2

I am using this block of code for mannual logout but i want automatic logout after 5 minutes of inactivity on website. How to do that? Thanks

<?php
session_start();
error_reporting(E_ALL);
ini_set('display_errors', '1');
session_destroy();

if(!session_is_registered('username')) {
  header("location: logout_msg.html");
} else {
  print "<h2>Could not log you out, sorry the system encountered an error.</h2>";
}

exit();
?> 
KyleK
  • 4,643
  • 17
  • 33
FarwallGhost
  • 688
  • 3
  • 7
  • 14

3 Answers3

1

You can try something like $_SESSION['CREATED'] to store a timestamp and then check if your value was created before $_SESSION['CREATED'] + 300.

Cedric Morent
  • 1,719
  • 1
  • 14
  • 25
0

Save the current timestamp in the session on every pageview. Check the value and invalidate their session if the current timestamp is more than five minutes different than the session value.

ceejayoz
  • 176,543
  • 40
  • 303
  • 368
0

It all depends on how you define inactivity. I do something like this

if ($_SESSION['LAST_ACTIVITY'] < time() - (60 * 5)) {
    // logout
}

$_SESSION['LAST_ACTIVITY'] = time();
danronmoon
  • 3,814
  • 5
  • 34
  • 56