title: how to make user logout after 30 mins of inactivity?
body: <p>I am using sessions for user login &amp; logout. I have a requirement that after 30 minutes of user inactivity he/she has to logout automatically. I searched &amp; tried few solutions but didn't work though. I tried below solutions:</p>
  
  <p>Solution1:</p>
  
  <pre><code>if(time() - $_SESSION['timestamp'] &gt; 900) { //subtract new timestamp from the old one
      echo"&lt;script&gt;alert('15 Minutes over!');&lt;/script&gt;";
      unset($_SESSION['email'], $_SESSION['user_id'], $_SESSION['timestamp']);
      session_destroy();
      $_SESSION['logged_in'] = false;
      header("Location: " . index.php); //redirect to index.php
      exit;
  } else {
    $_SESSION['timestamp'] = time(); //set new timestamp
  }
  </code></pre>
  
  <p>Solution2:</p>
  
  <pre><code>function auto_logout($field)
  {
    $t = time();
    $t0 = $_SESSION[$field];
    $diff = $t - $t0;
    if ($diff &gt; 3000 || !isset($t0))
    {          
      return true;
    }
    else
    {
      $_SESSION[$field] = time();
    }
  }
  if(auto_logout("email"))
  {
    session_unset();
    session_destroy();
    header('Location: index.php');
    exit;
  }
  </code></pre>
  
  <p>Neither of them worked, Could any one please tell me how to track last activity of user and check that time with the current time if exceeds 30 minutes and make that user logout? </p>