function SessionManager::save

Same name and namespace in other branches
  1. 11.x core/lib/Drupal/Core/Session/SessionManager.php \Drupal\Core\Session\SessionManager::save()
  2. 10 core/lib/Drupal/Core/Session/SessionManager.php \Drupal\Core\Session\SessionManager::save()
  3. 9 core/lib/Drupal/Core/Session/SessionManager.php \Drupal\Core\Session\SessionManager::save()
  4. 8.9.x core/lib/Drupal/Core/Session/SessionManager.php \Drupal\Core\Session\SessionManager::save()

File

core/lib/Drupal/Core/Session/SessionManager.php, line 143

Class

SessionManager
Manages user sessions.

Namespace

Drupal\Core\Session

Code

public function save() : void {
  if ($this->isCli()) {
    // We don't have anything to do if we are not allowed to save the session.
    return;
  }
  if ($this->isSessionObsolete()) {
    // There is no session data to store, destroy the session if it was
    // previously started.
    if ($this->getSaveHandler()
      ->isActive()) {
      $this->destroy();
    }
  }
  else {
    // There is session data to store. Start the session if it is not already
    // started.
    if (!$this->getSaveHandler()
      ->isActive()) {
      $this->startNow();
    }
    // Write the session data.
    parent::save();
  }
  $allowedKeys = array_map(fn(SessionBagInterface $bag) => $bag->getStorageKey(), $this->bags);
  $allowedKeys[] = $this->getMetadataBag()
    ->getStorageKey();
  $deprecatedKeys = array_diff(array_keys($_SESSION), $allowedKeys);
  if (count($deprecatedKeys) > 0) {
    @trigger_error(sprintf('Storing values directly in $_SESSION is deprecated in drupal:11.2.0 and will become unsupported in drupal:12.0.0. Use $request->getSession()->set() instead. Affected keys: %s. See https://www.drupal.org/node/3518527', implode(", ", $deprecatedKeys)), E_USER_DEPRECATED);
  }
  $this->startedLazy = FALSE;
}

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