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.

1

1 Answer 1

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).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.