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\HttpFoundation
;
15 * Represents a cookie.
17 * @author Johannes M. Schmitt <schmittjoh@gmail.com>
30 * @param string $name The name of the cookie
31 * @param string $value The value of the cookie
32 * @param int|string|\DateTime|\DateTimeInterface $expire The time the cookie expires
33 * @param string $path The path on the server in which the cookie will be available on
34 * @param string $domain The domain that the cookie is available to
35 * @param bool $secure Whether the cookie should only be transmitted over a secure HTTPS connection from the client
36 * @param bool $httpOnly Whether the cookie will be made accessible only through the HTTP protocol
38 * @throws \InvalidArgumentException
40 public function __construct($name, $value = null, $expire = 0, $path = '/', $domain = null, $secure = false, $httpOnly = true)
42 // from PHP source code
43 if (preg_match("/[=,; \t\r\n\013\014]/", $name)) {
44 throw new \
InvalidArgumentException(sprintf('The cookie name "%s" contains invalid characters.', $name));
48 throw new \
InvalidArgumentException('The cookie name cannot be empty.');
51 // convert expiration time to a Unix timestamp
52 if ($expire instanceof \DateTime ||
$expire instanceof \DateTimeInterface
) {
53 $expire = $expire->format('U');
54 } elseif (!is_numeric($expire)) {
55 $expire = strtotime($expire);
57 if (false === $expire) {
58 throw new \
InvalidArgumentException('The cookie expiration time is not valid.');
63 $this->value
= $value;
64 $this->domain
= $domain;
65 $this->expire
= 0 < $expire ?
(int) $expire : 0;
66 $this->path
= empty($path) ?
'/' : $path;
67 $this->secure
= (bool) $secure;
68 $this->httpOnly
= (bool) $httpOnly;
72 * Returns the cookie as a string.
74 * @return string The cookie
76 public function __toString()
78 $str = urlencode($this->getName()).'=';
80 if ('' === (string) $this->getValue()) {
81 $str .= 'deleted; expires='.gmdate('D, d-M-Y H:i:s T', time() - 31536001);
83 $str .= rawurlencode($this->getValue());
85 if (0 !== $this->getExpiresTime()) {
86 $str .= '; expires='.gmdate('D, d-M-Y H:i:s T', $this->getExpiresTime());
91 $str .= '; path='.$this->path
;
94 if ($this->getDomain()) {
95 $str .= '; domain='.$this->getDomain();
98 if (true === $this->isSecure()) {
102 if (true === $this->isHttpOnly()) {
103 $str .= '; httponly';
110 * Gets the name of the cookie.
114 public function getName()
120 * Gets the value of the cookie.
124 public function getValue()
130 * Gets the domain that the cookie is available to.
134 public function getDomain()
136 return $this->domain
;
140 * Gets the time the cookie expires.
144 public function getExpiresTime()
146 return $this->expire
;
150 * Gets the path on the server in which the cookie will be available on.
154 public function getPath()
160 * Checks whether the cookie should only be transmitted over a secure HTTPS connection from the client.
164 public function isSecure()
166 return $this->secure
;
170 * Checks whether the cookie will be made accessible only through the HTTP protocol.
174 public function isHttpOnly()
176 return $this->httpOnly
;
180 * Whether this cookie is about to be cleared.
184 public function isCleared()
186 return $this->expire
< time();