2

It´s possible to set session timeout by user in php?

Example: 2 users are registred in my site. I want that each user can set their own session timeout.

brpaz
  • 3,618
  • 9
  • 48
  • 75
  • 1
    Related: [How do I expire a PHP session after 30 minutes?](http://stackoverflow.com/questions/520237/how-do-i-expire-a-php-session-after-30-minutes/1270960#1270960) – Gumbo Jan 04 '11 at 17:09

1 Answers1

5

Yes, you can set a custom session timeout for each user. You can use the method as described in How do I expire a PHP session after 30 minutes? but store the absolute expiration time instead:

// set expiration time
$_SESSION['EXPIRES'] = time() + $customSessionLifetime;

// validate session
if (isset($_SESSION['EXPIRES']) && (time() < $_SESSION['EXPIRES'])) {
    // session still valid; update expiration time
    $_SESSION['EXPIRES'] = time() + $customSessionLifetime;
} else {
    // session invalid or expired
    session_destroy();
    session_unset();
}

Here $customSessionLifetime can be set differently for each user. Just make sure that its value is less than or equal to session.gc_maxlifetime and session.cookie_lifetime (if you use a cookie for the session ID).

Community
  • 1
  • 1
Gumbo
  • 643,351
  • 109
  • 780
  • 844