views:

78

answers:

3

I am creating a session when a user logs in like so:

$_SESSION['id'] = $id;

How can I specify a timeout on that session of X minutes and then have it perform a function or a page redirect once it has reached X minutes??

EDIT: I forgot to mention that I need the session to timeout due to inactivity.

A: 

When the session expires the data is no longer present, so something like

if (!isset($_SESSION['id'])) {
    Header("Location: destination.php");
}

will redirect whenever the session is no longer active.

You can set how long the session cookie is alive using session.cookie_lifetime

ini_set("session.cookie_lifetime","3600"); //an hour
Vinko Vrsalovic
you have to remove the quotes around $_SESSION['id']
Victor Stanciu
+1  A: 

first, store the last time the user made a request

<?php
  $_SESSION['timeout'] = time();
?>

in subsequent request, check how long ago they made their previous request (10 minutes in this example)

<?php
  if ($_SESSION['timeout'] + 10 * 60 < time()) {
     // session timed out
  } else {
     // session ok
  }
?>
Jacco
A: 

You can set timeout with javascript setTimeout function and call a php file to redirect it.

shinod
the good thing about sessions is that they are server side.
Jacco
Yea, I think we can unset session in that php file.
shinod