axit sompura axit sompura -4 years ago 62
PHP Question

How to set expire time in session like as cookie in php?

Hi if i want to use cookie it syntax like below also in this option we can able to set expire time of cookie.

syntax:

setcookie(name, value, expire, path, domain, secure, httponly);


it is possible to set expire time in session?

Answer Source
//Start our session.
session_start();

//Expire the session if user is inactive for 30
//minutes or more.
$expireAfter = 30;

//Check to see if our "last action" session
//variable has been set.
if(isset($_SESSION['last_action'])){

    //Figure out how many seconds have passed
    //since the user was last active.
    $secondsInactive = time() - $_SESSION['last_action'];

    //Convert our minutes into seconds.
    $expireAfterSeconds = $expireAfter * 60;

    //Check to see if they have been inactive for too long.
    if($secondsInactive >= $expireAfterSeconds){
        //User has been inactive for too long.
        //Kill their session.
        session_unset();
        session_destroy();
    }

}

//Assign the current timestamp as the user's
//latest activity
$_SESSION['last_action'] = time();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download