You are here

protected function SessionBasedTempStore::getOwner in Session Based Temporary Storage 8

Gets the current owner based on the current session ID.

Return value

string The owner.

Throws

\Drupal\Core\TempStore\TempStoreException Thrown when headers have been already send.

5 calls to SessionBasedTempStore::getOwner()
SessionBasedTempStore::createkey in src/SessionBasedTempStore.php
Ensures that the key is unique for a user.
SessionBasedTempStore::delete in src/SessionBasedTempStore.php
Deletes data from the store for a given key and releases the lock on it.
SessionBasedTempStore::get in src/SessionBasedTempStore.php
Retrieves a value from this PrivateTempStore for a given key.
SessionBasedTempStore::getAll in src/SessionBasedTempStore.php
Retrieves an array of all values from this PrivateTempStore.
SessionBasedTempStore::set in src/SessionBasedTempStore.php
Stores a particular key/value pair in this PrivateTempStore.

File

src/SessionBasedTempStore.php, line 276

Class

SessionBasedTempStore
Stores and retrieves temporary data for a given owner.

Namespace

Drupal\session_based_temp_store

Code

protected function getOwner() {
  if (empty($_COOKIE[$this->cookieName])) {

    // Since security is not a problem, let's keep it short.
    $session_store_id = mb_substr(session_id(), 0, 12);
    $request = $this->requestStack
      ->getCurrentRequest();
    $session_options = $this->sessionConfiguration
      ->getOptions($request);

    // .localhost causes problems.
    $cookie_domain = $session_options['cookie_domain'] == '.localhost' ? ini_get('session.cookie_domain') : $session_options['cookie_domain'];

    // If the site is accessed via SSL, ensure that the cookie is issued
    // with the secure flag.
    $secure = $request
      ->isSecure();

    // setcookie() can only be called when headers are not yet sent.
    if (!headers_sent()) {
      setcookie($this->cookieName, $session_store_id, $this
        ->expirationTime(), $this->path, $cookie_domain, $secure, TRUE);
    }
    else {
      throw new TempStoreException("Couldn't set cookie " . $this->cookieName . " in session based temporary storage. Headers have been already sent.");
    }

    // When sessionStoreId() is called multiple times
    // and there is no $_COOKIE[$this->cookieName] yet.
    // The multiple identical Set-Cookie headers are sent to the client.
    // So we need to set $_COOKIE explicitly.
    $_COOKIE[$this->cookieName] = $session_store_id;
  }
  else {
    $session_store_id = $_COOKIE[$this->cookieName];
  }
  return $session_store_id;
}