Steve Bals
Steve Bals

Reputation: 1979

Session auto expiry if user in idle

I need to destroy the session if the user is idle up to 10 minutes also how to find the time of last activity of the user ,

if ($_SESSION['last_activity'] > 600)
{
 session_unset();   
 session_destroy();
}

is this correct way..

Upvotes: 1

Views: 599

Answers (3)

user1844933
user1844933

Reputation: 3417

Use session.gc_maxlifetime

Set session.gc_maxlifetime = 600 in phi.ini

or

ini_set('session.gc_maxlifetime',600); // in your script

Upvotes: 2

Cerbrus
Cerbrus

Reputation: 72857

Check for activity before resetting the time:

if (isset($_SESSION['last_activity']) && $_SESSION['last_activity'] > 600){
    session_unset();   
    session_destroy();
}else{
    $_SESSION['last_activity'] = time();
}

Upvotes: 0

Dhanush Bala
Dhanush Bala

Reputation: 33

//on pageload
session_start();

$idletime=60;//after 60 seconds the user gets logged out

if (time()-$_SESSION['timestamp']>$idletime){
    session_destroy();
    session_unset();
}else{
    $_SESSION['timestamp']=time();
}

//on session creation
$_SESSION['timestamp']=time();

Upvotes: 0

Related Questions