protected function setCookieFromSession($session = NULL) {
    if (!$this->getVariable('cookie_support'))
      return;

    $cookie_name = $this->getSessionCookieName();
    $value = 'deleted';
    $expires = time() - 3600;
    $base_domain = $this->getVariable('base_domain', self::DEFAULT_BASE_DOMAIN);
    if ($session) {
      $value = '"' . http_build_query($session, NULL, '&') . '"';
      $base_domain = isset($session['base_domain']) ? $session['base_domain'] : $base_domain;
      $expires = isset($session['expires']) ? $session['expires'] : time() + $this->getVariable('expires_in', self::DEFAULT_EXPIRES_IN);
    }

    // Prepend dot if a domain is found.
    if ($base_domain)
      $base_domain = '.' . $base_domain;

    // If an existing cookie is not set, we dont need to delete it.
    if ($value == 'deleted' && empty($_COOKIE[$cookie_name]))
      return;

    if (headers_sent())
      error_log('Could not set cookie. Headers already sent.');
    else
      setcookie($cookie_name, $value, $expires, '/', $base_domain);
  }