logan
logan

Reputation: 8366

PHP session is getting expired for every 1 hour automatically

I am setting up a PHP session to be valid for 4hr after login. but session is getting expired exactly at rounded hours (@1 AM, 2 AM, 3 AM so on..)

For eg: if login at 12.00 AM or 12.30AM, session is getting expired at 1 AM exactly.

Is there any default setting done in shared hosting for session ?

I have tried changing session save path and session life but NO use.

Here is my code during login page.

// server should keep session data for AT LEAST 4 hour
ini_set(session.save_path, "/home/web/session"); 
ini_set('session.gc_maxlifetime', 14400);
session_start();
$_SESSION['login'] = 1;
$_SESSION['sessionid']=session_id();

And in other pages:

if($_SESSION['login'] == 1  $_SESSION['sessionid']==session_id()) 
{ 
    echo "you are aleady logged in...";
}
else
{ echo "you are not logged in..." ; //this is occurring exactly every hour
}

Could you please help me to resolve this issue ?

Upvotes: 0

Views: 2236

Answers (1)

robinp7720
robinp7720

Reputation: 443

Session_start() must be before everything else. That is probably your problem. And anyways that way is very unreliable so its best to crate your own session timeout:

if (isset($_SESSION['LAST_ACTIVITY']) && (time() - $_SESSION['LAST_ACTIVITY'] > 14400)) {
    // last request was more than 30 minutes ago
    session_unset();     // unset $_SESSION variable for the run-time 
    session_destroy();   // destroy session data in storage
}
$_SESSION['LAST_ACTIVITY'] = time(); // update last activity time stamp

Upvotes: 1

Related Questions