Maha Dev
Maha Dev

Reputation: 3965

PHP Session increase expiry time

I just want to extend the php session time in my project. From some documentation i found this code:

ini_set('session.gc_maxlifetime', 2*60*60); //2 hours

By default , i think its 30 minutes. But I need to increase this. So for testing I set this time for just 2 minutes and tried my code. I waited for 2 minutes, but i didn't expires.

I want my session for more than 5 hours. But my code is not working. I am using this ini_set inside my php file rather than in php_ini

Upvotes: 0

Views: 261

Answers (1)

xcution
xcution

Reputation: 91

// server should keep session data for AT LEAST 5 hour

ini_set('session.gc_maxlifetime', 18000);

// each client should remember their session id for EXACTLY 5 hour

session_set_cookie_params(18000);

session_start(); // ready to go!

THEN

$now = time();
if (isset($_SESSION['discard_after']) && $now > $_SESSION['discard_after']) {
    // this session has worn out its welcome; kill it and start a brand new one
    session_unset();
    session_destroy();
    session_start();
}

// either new or old, it should live at most for another hour
$_SESSION['discard_after'] = $now + 18000;

Upvotes: 3

Related Questions