4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\BrowserKit;
15 * Cookie represents an HTTP cookie.
17 * @author Fabien Potencier <fabien@symfony.com>
22 * Handles dates as defined by RFC 2616 section 3.3.1, and also some other
23 * non-standard, but common formats.
25 private static $dateFormats = array(
47 * @param string $name The cookie name
48 * @param string $value The value of the cookie
49 * @param string|null $expires The time the cookie expires
50 * @param string|null $path The path on the server in which the cookie will be available on
51 * @param string $domain The domain that the cookie is available
52 * @param bool $secure Indicates that the cookie should only be transmitted over a secure HTTPS connection from the client
53 * @param bool $httponly The cookie httponly flag
54 * @param bool $encodedValue Whether the value is encoded or not
56 public function __construct($name, $value, $expires = null, $path = null, $domain = '', $secure = false, $httponly = true, $encodedValue = false)
59 $this->value = urldecode($value);
60 $this->rawValue = $value;
62 $this->value = $value;
63 $this->rawValue = rawurlencode($value);
66 $this->path = empty($path) ? '/' : $path;
67 $this->domain = $domain;
68 $this->secure = (bool) $secure;
69 $this->httponly = (bool) $httponly;
71 if (null !== $expires) {
72 $timestampAsDateTime = \DateTime::createFromFormat('U', $expires);
73 if (false === $timestampAsDateTime) {
74 throw new \UnexpectedValueException(sprintf('The cookie expiration time "%s" is not valid.', $expires));
77 $this->expires = $timestampAsDateTime->format('U');
82 * Returns the HTTP representation of the Cookie.
84 public function __toString()
86 $cookie = sprintf('%s=%s', $this->name, $this->rawValue);
88 if (null !== $this->expires) {
89 $dateTime = \DateTime::createFromFormat('U', $this->expires, new \DateTimeZone('GMT'));
90 $cookie .= '; expires='.str_replace('+0000', '', $dateTime->format(self::$dateFormats[0]));
93 if ('' !== $this->domain) {
94 $cookie .= '; domain='.$this->domain;
98 $cookie .= '; path='.$this->path;
102 $cookie .= '; secure';
105 if ($this->httponly) {
106 $cookie .= '; httponly';
113 * Creates a Cookie instance from a Set-Cookie header value.
115 * @param string $cookie A Set-Cookie header value
116 * @param string|null $url The base URL
120 * @throws \InvalidArgumentException
122 public static function fromString($cookie, $url = null)
124 $parts = explode(';', $cookie);
126 if (false === strpos($parts[0], '=')) {
127 throw new \InvalidArgumentException(sprintf('The cookie string "%s" is not valid.', $parts[0]));
130 list($name, $value) = explode('=', array_shift($parts), 2);
133 'name' => trim($name),
134 'value' => trim($value),
140 'passedRawValue' => true,
144 if ((false === $urlParts = parse_url($url)) || !isset($urlParts['host'])) {
145 throw new \InvalidArgumentException(sprintf('The URL "%s" is not valid.', $url));
148 $values['domain'] = $urlParts['host'];
149 $values['path'] = isset($urlParts['path']) ? substr($urlParts['path'], 0, strrpos($urlParts['path'], '/')) : '';
152 foreach ($parts as $part) {
155 if ('secure' === strtolower($part)) {
156 // Ignore the secure flag if the original URI is not given or is not HTTPS
157 if (!$url || !isset($urlParts['scheme']) || 'https' != $urlParts['scheme']) {
161 $values['secure'] = true;
166 if ('httponly' === strtolower($part)) {
167 $values['httponly'] = true;
172 if (2 === \count($elements = explode('=', $part, 2))) {
173 if ('expires' === strtolower($elements[0])) {
174 $elements[1] = self::parseDate($elements[1]);
177 $values[strtolower($elements[0])] = $elements[1];
189 $values['passedRawValue']
193 private static function parseDate($dateValue)
195 // trim single quotes around date if present
196 if (($length = \strlen($dateValue)) > 1 && "'" === $dateValue[0] && "'" === $dateValue[$length - 1]) {
197 $dateValue = substr($dateValue, 1, -1);
200 foreach (self::$dateFormats as $dateFormat) {
201 if (false !== $date = \DateTime::createFromFormat($dateFormat, $dateValue, new \DateTimeZone('GMT'))) {
202 return $date->format('U');
206 // attempt a fallback for unusual formatting
207 if (false !== $date = date_create($dateValue, new \DateTimeZone('GMT'))) {
208 return $date->format('U');
213 * Gets the name of the cookie.
215 * @return string The cookie name
217 public function getName()
223 * Gets the value of the cookie.
225 * @return string The cookie value
227 public function getValue()
233 * Gets the raw value of the cookie.
235 * @return string The cookie value
237 public function getRawValue()
239 return $this->rawValue;
243 * Gets the expires time of the cookie.
245 * @return string|null The cookie expires time
247 public function getExpiresTime()
249 return $this->expires;
253 * Gets the path of the cookie.
255 * @return string The cookie path
257 public function getPath()
263 * Gets the domain of the cookie.
265 * @return string The cookie domain
267 public function getDomain()
269 return $this->domain;
273 * Returns the secure flag of the cookie.
275 * @return bool The cookie secure flag
277 public function isSecure()
279 return $this->secure;
283 * Returns the httponly flag of the cookie.
285 * @return bool The cookie httponly flag
287 public function isHttpOnly()
289 return $this->httponly;
293 * Returns true if the cookie has expired.
295 * @return bool true if the cookie has expired, false otherwise
297 public function isExpired()
299 return null !== $this->expires && 0 != $this->expires && $this->expires < time();