public function testPrefersLeftmostCookieWhenManyCookiesWithSameName() { $header = 'foo=bar; foo=beer'; $result = Slim_Http_Util::parseCookieHeader($header); $this->assertEquals('bar', $result['foo']); }
/** * Save session * @return void */ protected function saveSession() { $value = Slim_Http_Util::encodeSecureCookie(serialize($_SESSION), $this->settings['expires'], $this->settings['secret'], $this->settings['cipher'], $this->settings['cipher_mode']); if (strlen($value) > 4096) { $this->app->getLog()->error('WARNING! Slim_Middleware_SessionCookie data size is larger than 4KB. Content save failed.'); } else { $this->app->response()->setCookie($this->settings['name'], array('value' => $value, 'domain' => $this->settings['domain'], 'path' => $this->settings['path'], 'expires' => $this->settings['expires'], 'secure' => $this->settings['secure'], 'httponly' => $this->settings['httponly'])); } session_destroy(); }
/** * Fetch COOKIE data * * This method returns a key-value array of Cookie data sent in the HTTP request, or * the value of a array key if requested; if the array key does not exist, NULL is returned. * * @param string $key * @return array|string|null */ public function cookies($key = null) { if (!isset($this->env['slim.request.cookie_hash'])) { $cookieHeader = isset($this->env['COOKIE']) ? $this->env['COOKIE'] : ''; $this->env['slim.request.cookie_hash'] = Slim_Http_Util::parseCookieHeader($cookieHeader); } if ($key) { if (isset($this->env['slim.request.cookie_hash'][$key])) { return $this->env['slim.request.cookie_hash'][$key]; } else { return null; } } else { return $this->env['slim.request.cookie_hash']; } }
/** * Get the value of an encrypted Cookie from the current HTTP request * * Return the value of an encrypted cookie from the current HTTP request, * or return NULL if cookie does not exist. Encrypted cookies created during * the current request will not be available until the next request. * * @param string $name * @return string|false */ public function getEncryptedCookie($name, $deleteIfInvalid = true) { $value = Slim_Http_Util::decodeSecureCookie($this->request->cookies($name), $this->config('cookies.secret_key'), $this->config('cookies.cipher'), $this->config('cookies.cipher_mode')); if ($value === false && $deleteIfInvalid) { $this->deleteCookie($name); } return $value; }
/** * Delete cookie * * Instead of using PHP's `setcookie()` function, Slim manually constructs the HTTP `Set-Cookie` * header on its own and delegates this responsibility to the `Slim_Http_Util` class. This * response's header is passed by reference to the utility class and is directly modified. By not * relying on PHP's native implementation, Slim allows middleware the opportunity to massage or * analyze the raw header before the response is ultimately delivered to the HTTP client. * * This method will set a cookie with the given name that has an expiration time in the past; this will * prompt the HTTP client to invalidate and remove the client-side cookie. Optionally, you may * also pass a key/value array as the second argument. If the "domain" key is present in this * array, only the Cookie with the given name AND domain will be removed. The invalidating cookie * sent with this response will adopt all properties of the second argument. * * @param string $name The name of the cookie * @param array $value Properties for cookie including: value, expire, path, domain, secure, httponly */ public function deleteCookie($name, $value = array()) { Slim_Http_Util::deleteCookieHeader($this->header, $name, $value); }
/** * Save session * @param int $status * @param array $header * @param string $body * @return array[status, header, body] */ protected function saveSession(&$env, $status, $header, $body) { $r = new Slim_Http_Response($body, $status, $header); $value = Slim_Http_Util::encodeSecureCookie(serialize($_SESSION), $this->settings['expires'], $this->settings['secret'], $this->settings['cipher'], $this->settings['cipher_mode']); if (strlen($value) > 4096) { fwrite($env['slim.errors'], 'WARNING! Slim_Middleware_SessionCookie data size is larger than 4KB. Content save failed.'); } else { $r->setCookie($this->settings['name'], $value, $this->settings['expires'], $this->settings['path'], $this->settings['domain'], $this->settings['secure'], $this->settings['httponly']); } return $r->finalize(); }
/** * Test parses Cookie: HTTP header */ public function testParsesCookieHeader() { $header = 'foo=bar; one=two; colors=blue; colors=red; colors=green'; $result = Slim_Http_Util::parseCookieHeader($header); $this->assertEquals(3, count($result)); $this->assertEquals('bar', $result['foo']); $this->assertEquals('two', $result['one']); $this->assertEquals(3, count($result['colors'])); $this->assertEquals('blue', $result['colors'][0]); $this->assertEquals('red', $result['colors'][1]); $this->assertEquals('green', $result['colors'][2]); }