Expire session when there is no activity in PHP

I found many tutorials on the Internet when you expire a session after a certain limit, for example, after 30 minutes or so. But I want to expire the session when there is no activity, a quote from the well-known question SO solution directly:

if (isset($_SESSION['LAST_ACTIVITY']) 
    && (time() - $_SESSION['LAST_ACTIVITY'] > 1800)) {
        // 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

but do I need to update $_SESSION['LAST_ACTIVITY']for every request?

Alleged answer: Yes, but I have a large site containing 200 + php pages, and it is very difficult to update $_SESSION['LAST_ACTIVITY']for each request.

Is there any other way to do this? The only thing in common among all the files is one configuration file for connecting to the DB.

+4
2

$_SESSION['LAST_ACTIVITY'] () , 30 .

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

- , , 200 php .

+6

@Simon Fischer: "> 1800))".

0

All Articles