PHP Expiration

Question about session expiration in PHP.

I need my server to delete session information if this user is inactive for some time (for testing purposes - 5 seconds).

I reviewed this question and, in particular, the answer to the Gumbo question (+28 votes), and I wondered about the appropriateness of this answer with respect for inactive users. On my site, I have already implemented this proposal, and it works fine if the user requests some data at least once after the end of the session. But the problem with inactive users is that they are not requesting new data. Therefore, the expiration code is never called.

I looked session.gc_maxlifeand related parameters in my PHP.ini, but I could not do this work the way I wanted it.

Any suggestions on this issue?

+3
source share
4 answers

If you need to invoke certain expiration logic (for example, to update the database) and get query independence, then it would be wise to implement an external session handler that handles access time to session files. The daemon script must execute everything necessary for each session file that has not been accessed for the specified time.

: (Windows ), .

+1

, , , : , .

, , , ; , . session.gc_probability, session. gc_divisor session_start (. ?).


. , .

: , , :

interface SessionSaveHandler {
    public function open();
    public function close();
    public function read($id)
    public function write($id, $data);
    public function destroy($id);
    public function gc($callback=null);
}
class SessionSaveHandler_WithAdditionalTasks implements SessionSaveHandler {
    // …
    public function gc($callback=null) {
        if (!is_null($callback) && (!is_array($callback) || !is_callable($callback))) return false;
        while (/* … */) {
            if ($callback) $callback[0]::$callback[1]($id);
            // destroy expired sessions
            // …
        }
    }
    public static function doAdditionalTasksOn($id) {
        // additional tasks with $id
    }
}
session_set_save_handler(array('SessionSaveHandler_DB_WithAdditionalTasks', 'open'),
                         array('SessionSaveHandler_DB_WithAdditionalTasks', 'close'),
                         array('SessionSaveHandler_DB_WithAdditionalTasks', 'read'),
                         array('SessionSaveHandler_DB_WithAdditionalTasks', 'write'),
                         array('SessionSaveHandler_DB_WithAdditionalTasks', 'destroy'),
                         array('SessionSaveHandler_DB_WithAdditionalTasks', 'gc')
                         );
+3

, , tge gc_ *, . , - , . , - , cronjob, - , - .

+1

One way to do this is to use javascript to refresh the page a bit after a wait period. Of course, your users must have Javascript for this. You can also add additional features, such as the presence of javascript with a timeout notification with a countdown, etc. So what matters is what happens, the session has expired due to your settings, and then updates, cleanup, and execution.

<html>
<head>
<script type="text/JavaScript">
<!--
function timedRefresh(timeoutPeriod) {
    setTimeout("location.reload(true);",timeoutPeriod);
}
//   -->
</script>
</head>
<body onload="JavaScript:timedRefresh(5000);">
</body>
</html>
+1
source

Source: https://habr.com/ru/post/1763094/


All Articles