W3cubDocs

/Drupal 8

public function SessionHandler::write

public SessionHandler::write($sid, $value)

Writes the session data to the storage.

Care, the session ID passed to write() can be different from the one previously received in read() when the session ID changed due to session_regenerate_id().

Parameters

string $sessionId Session ID , see http://php.net/function.session-id:

string $data Serialized session data to save:

Return value

bool true on success, false on failure

Overrides SessionHandlerInterface::write

See also

http://php.net/sessionhandlerinterface.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 = array(
      'uid' => $request->getSession()->get('uid', 0),
      'hostname' => $request->getClientIP(),
      'session' => $value,
      'timestamp' => REQUEST_TIME,
    );
    $this->connection->merge('sessions')
      ->keys(array('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;
  }
}

© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Session!SessionHandler.php/function/SessionHandler::write/8.1.x