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>
32 * @param string $name The name of the cookie
33 * @param string $value The value of the cookie
34 * @param int|string|\DateTime|\DateTimeInterface $expire The time the cookie expires
35 * @param string $path The path on the server in which the cookie will be available on
36 * @param string $domain The domain that the cookie is available to
37 * @param bool $secure Whether the cookie should only be transmitted over a secure HTTPS connection from the client
38 * @param bool $httpOnly Whether the cookie will be made accessible only through the HTTP protocol
40 * @throws \InvalidArgumentException
42 public function __construct($name, $value = null, $expire = 0, $path = '/', $domain = null, $secure = false, $httpOnly = true)
44 // from PHP source code
45 if (preg_match("/[=,; \t\r\n\013\014]/", $name)) {
46 throw new \InvalidArgumentException(sprintf('The cookie name "%s" contains invalid characters.', $name));
50 throw new \InvalidArgumentException('The cookie name cannot be empty.');
53 // convert expiration time to a Unix timestamp
54 if ($expire instanceof \DateTime || $expire instanceof \DateTimeInterface) {
55 $expire = $expire->format('U');
56 } elseif (!is_numeric($expire)) {
57 $expire = strtotime($expire);
59 if (false === $expire) {
60 throw new \InvalidArgumentException('The cookie expiration time is not valid.');
65 $this->value = $value;
66 $this->domain = $domain;
67 $this->expire = 0 < $expire ? (int) $expire : 0;
68 $this->path = empty($path) ? '/' : $path;
69 $this->secure = (bool) $secure;
70 $this->httpOnly = (bool) $httpOnly;
74 * Returns the cookie as a string.
76 * @return string The cookie
78 public function __toString()
80 $str = urlencode($this->getName()).'=';
82 if ('' === (string) $this->getValue()) {
83 $str .= 'deleted; expires='.gmdate('D, d-M-Y H:i:s T', time() - 31536001);
85 $str .= urlencode($this->getValue());
87 if (0 !== $this->getExpiresTime()) {
88 $str .= '; expires='.gmdate('D, d-M-Y H:i:s T', $this->getExpiresTime());
93 $str .= '; path='.$this->path;
96 if ($this->getDomain()) {
97 $str .= '; domain='.$this->getDomain();
100 if (true === $this->isSecure()) {
104 if (true === $this->isHttpOnly()) {
105 $str .= '; httponly';
112 * Gets the name of the cookie.
116 public function getName()
122 * Gets the value of the cookie.
126 public function getValue()
132 * Gets the domain that the cookie is available to.
136 public function getDomain()
138 return $this->domain;
142 * Gets the time the cookie expires.
146 public function getExpiresTime()
148 return $this->expire;
152 * Gets the path on the server in which the cookie will be available on.
156 public function getPath()
162 * Checks whether the cookie should only be transmitted over a secure HTTPS connection from the client.
166 public function isSecure()
168 return $this->secure;
172 * Checks whether the cookie will be made accessible only through the HTTP protocol.
176 public function isHttpOnly()
178 return $this->httpOnly;
182 * Whether this cookie is about to be cleared.
186 public function isCleared()
188 return $this->expire < time();