AppserverIo\Appserver\ServletEngine\Session\FilesystemSessionHandler::collectGarbage PHP Method

collectGarbage() public method

Collects the garbage by deleting expired sessions.
public collectGarbage ( ) : integer
return integer The number of removed sessions
    public function collectGarbage()
    {
        // counter to store the number of removed sessions
        $sessionRemovalCount = 0;
        // we want to know what inactivity timeout we've to check the sessions for
        $inactivityTimeout = $this->getSessionSettings()->getInactivityTimeout();
        // prepare the expression to select the session files
        $globExpression = sprintf('%s*', $this->getSessionSavePath($this->getSessionSettings()->getSessionFilePrefix()));
        // iterate over the found session files
        foreach (glob($globExpression) as $pathname) {
            // unpersist the session
            $session = $this->unpersist($pathname);
            // load the sessions last activity timestamp
            $lastActivitySecondsAgo = time() - $session->getLastActivityTimestamp();
            // query whether or not the session has been expired
            if ($lastActivitySecondsAgo > $inactivityTimeout) {
                // if yes, delete the session + raise the session removal count
                $this->delete($session->getId());
                $sessionRemovalCount++;
            }
        }
        // return the number of removed sessions
        return $sessionRemovalCount;
    }