Search code examples
phpsessionexpired-sessions

Is this a viable session expire?


Basically what I wanted was to create a session with an expire time of 24 hours. And I wrote this:

session_start();
if (!isset($_SESSION['TIME_START']) || $_SESSION['TIME_START'] < time()) {
        $_SESSION['TIME_START'] = time();
} else if ($_SESSION['TIME_START'] > 60 * 60 * 24) {
    session_destroy();
}

I would like to know if this is a valid, viable or good practice to do it. Thanks!


Solution

  • 1 - A session lifetime, is set by default by the server side. Check session.gc_maxlifetime parameter in php.ini

    2 - As Rakesh Sharma said, maybe better to use Cookies

    3 - if (!isset($_SESSION['TIME_START']) || $_SESSION['TIME_START'] < time()) { in this case, you will reset your $_SESSION['TIME_START'] each time the user go through this function under the 24 hours requested in your needs. Is that wanted ?