function SessionHandler::write

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

File

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

Class

SessionHandler
Default session handler.

Namespace

Drupal\Core\Session

Code

public function write(string $sid, string $value) : bool {
    $try_again = FALSE;
    $request = $this->requestStack
        ->getCurrentRequest();
    $fields = [
        'uid' => $request->getSession()
            ->get('uid', 0),
        'hostname' => $request->getClientIP(),
        'session' => $value,
        'timestamp' => $this->time
            ->getRequestTime(),
    ];
    $doWrite = fn() => $this->connection
        ->merge('sessions')
        ->keys([
        'sid' => Crypt::hashBase64($sid),
    ])
        ->fields($fields)
        ->execute();
    try {
        $doWrite();
    } catch (\Exception $e) {
        // If there was an exception, try to create the table.
        if (!($try_again = $this->ensureTableExists())) {
            // If the exception happened for other reason than the missing
            // table, propagate the exception.
            throw $e;
        }
    }
    // Now that the bin has been created, try again if necessary.
    if ($try_again) {
        $doWrite();
    }
    return TRUE;
}

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