session_destroy() after certain amount of time in PHP

后端 未结 2 1283
闹比i
闹比i 2020-12-11 12:07

I am currently saving a session in my form page:

$testingvalue = "SESSION TEST";

$_SESSION[\'testing\'] = $testingvalue;

2条回答
  •  难免孤独
    2020-12-11 12:23

    you need a static start time to expire. $session_life > $inactive will always be greater no matter what.

    session_start();
    
    $testingvalue = "SESSION TEST";
    $_SESSION['testing'] = $testingvalue;
    
    // 2 hours in seconds
    $inactive = 7200;
    
    
    $_SESSION['expire'] = time() + $inactive; // static expire
    
    
    if(time() > $_SESSION['expire'])
    {  
    $_SESSION['testing'] = '';
     session_unset();
     session_destroy(); 
    $_SESSION['testing'] = '2 hours expired'; // test message
    }
    
    echo $_SESSION['testing'];
    

    or session-set-cookie-params

提交回复
热议问题