123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190 |
- <?php
- namespace Symfony\Component\HttpFoundation;
- class Cookie
- {
- protected $name;
- protected $value;
- protected $domain;
- protected $expire;
- protected $path;
- protected $secure;
- protected $httpOnly;
-
- public function __construct($name, $value = null, $expire = 0, $path = '/', $domain = null, $secure = false, $httpOnly = true)
- {
-
- if (preg_match("/[=,; \t\r\n\013\014]/", $name)) {
- throw new \InvalidArgumentException(sprintf('The cookie name "%s" contains invalid characters.', $name));
- }
- if (empty($name)) {
- throw new \InvalidArgumentException('The cookie name cannot be empty.');
- }
-
- if ($expire instanceof \DateTime || $expire instanceof \DateTimeInterface) {
- $expire = $expire->format('U');
- } elseif (!is_numeric($expire)) {
- $expire = strtotime($expire);
- if (false === $expire) {
- throw new \InvalidArgumentException('The cookie expiration time is not valid.');
- }
- }
- $this->name = $name;
- $this->value = $value;
- $this->domain = $domain;
- $this->expire = 0 < $expire ? (int) $expire : 0;
- $this->path = empty($path) ? '/' : $path;
- $this->secure = (bool) $secure;
- $this->httpOnly = (bool) $httpOnly;
- }
-
- public function __toString()
- {
- $str = urlencode($this->getName()).'=';
- if ('' === (string) $this->getValue()) {
- $str .= 'deleted; expires='.gmdate('D, d-M-Y H:i:s T', time() - 31536001);
- } else {
- $str .= urlencode($this->getValue());
- if (0 !== $this->getExpiresTime()) {
- $str .= '; expires='.gmdate('D, d-M-Y H:i:s T', $this->getExpiresTime());
- }
- }
- if ($this->path) {
- $str .= '; path='.$this->path;
- }
- if ($this->getDomain()) {
- $str .= '; domain='.$this->getDomain();
- }
- if (true === $this->isSecure()) {
- $str .= '; secure';
- }
- if (true === $this->isHttpOnly()) {
- $str .= '; httponly';
- }
- return $str;
- }
-
- public function getName()
- {
- return $this->name;
- }
-
- public function getValue()
- {
- return $this->value;
- }
-
- public function getDomain()
- {
- return $this->domain;
- }
-
- public function getExpiresTime()
- {
- return $this->expire;
- }
-
- public function getPath()
- {
- return $this->path;
- }
-
- public function isSecure()
- {
- return $this->secure;
- }
-
- public function isHttpOnly()
- {
- return $this->httpOnly;
- }
-
- public function isCleared()
- {
- return $this->expire < time();
- }
- }
|