function SessionHandler::write

Same name in other branches
  1. 9 core/lib/Drupal/Core/Session/SessionHandler.php \Drupal\Core\Session\SessionHandler::write()
  2. 10 core/lib/Drupal/Core/Session/SessionHandler.php \Drupal\Core\Session\SessionHandler::write()
  3. 11.x core/lib/Drupal/Core/Session/SessionHandler.php \Drupal\Core\Session\SessionHandler::write()

File

core/lib/Drupal/Core/Session/SessionHandler.php, line 70

Class

SessionHandler
Default session handler.

Namespace

Drupal\Core\Session

Code

public function write($sid, $value) {
    // The exception handler is not active at this point, so we need to do it
    // manually.
    try {
        $request = $this->requestStack
            ->getCurrentRequest();
        $fields = [
            'uid' => $request->getSession()
                ->get('uid', 0),
            'hostname' => $request->getClientIP(),
            'session' => $value,
            'timestamp' => REQUEST_TIME,
        ];
        $this->connection
            ->merge('sessions')
            ->keys([
            'sid' => Crypt::hashBase64($sid),
        ])
            ->fields($fields)
            ->execute();
        return TRUE;
    } catch (\Exception $exception) {
        require_once DRUPAL_ROOT . '/core/includes/errors.inc';
        // If we are displaying errors, then do so with no possibility of a
        // further uncaught exception being thrown.
        if (error_displayable()) {
            print '<h1>Uncaught exception thrown in session handler.</h1>';
            print '<p>' . Error::renderExceptionSafe($exception) . '</p><hr />';
        }
        return FALSE;
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.