[ Index ] |
PHP Cross Reference of phpBB-3.2.11-deutsch |
[Summary view] [Print] [Text view]
1 <?php 2 3 /* 4 * This file is part of the Symfony package. 5 * 6 * (c) Fabien Potencier <fabien@symfony.com> 7 * 8 * For the full copyright and license information, please view the LICENSE 9 * file that was distributed with this source code. 10 */ 11 12 namespace Symfony\Component\HttpFoundation; 13 14 /** 15 * Represents a cookie. 16 * 17 * @author Johannes M. Schmitt <schmittjoh@gmail.com> 18 */ 19 class Cookie 20 { 21 protected $name; 22 protected $value; 23 protected $domain; 24 protected $expire; 25 protected $path; 26 protected $secure; 27 protected $httpOnly; 28 29 /** 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 37 * 38 * @throws \InvalidArgumentException 39 */ 40 public function __construct($name, $value = null, $expire = 0, $path = '/', $domain = null, $secure = false, $httpOnly = true) 41 { 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)); 45 } 46 47 if (empty($name)) { 48 throw new \InvalidArgumentException('The cookie name cannot be empty.'); 49 } 50 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); 56 57 if (false === $expire) { 58 throw new \InvalidArgumentException('The cookie expiration time is not valid.'); 59 } 60 } 61 62 $this->name = $name; 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; 69 } 70 71 /** 72 * Returns the cookie as a string. 73 * 74 * @return string The cookie 75 */ 76 public function __toString() 77 { 78 $str = urlencode($this->getName()).'='; 79 80 if ('' === (string) $this->getValue()) { 81 $str .= 'deleted; expires='.gmdate('D, d-M-Y H:i:s T', time() - 31536001); 82 } else { 83 $str .= rawurlencode($this->getValue()); 84 85 if (0 !== $this->getExpiresTime()) { 86 $str .= '; expires='.gmdate('D, d-M-Y H:i:s T', $this->getExpiresTime()); 87 } 88 } 89 90 if ($this->path) { 91 $str .= '; path='.$this->path; 92 } 93 94 if ($this->getDomain()) { 95 $str .= '; domain='.$this->getDomain(); 96 } 97 98 if (true === $this->isSecure()) { 99 $str .= '; secure'; 100 } 101 102 if (true === $this->isHttpOnly()) { 103 $str .= '; httponly'; 104 } 105 106 return $str; 107 } 108 109 /** 110 * Gets the name of the cookie. 111 * 112 * @return string 113 */ 114 public function getName() 115 { 116 return $this->name; 117 } 118 119 /** 120 * Gets the value of the cookie. 121 * 122 * @return string 123 */ 124 public function getValue() 125 { 126 return $this->value; 127 } 128 129 /** 130 * Gets the domain that the cookie is available to. 131 * 132 * @return string 133 */ 134 public function getDomain() 135 { 136 return $this->domain; 137 } 138 139 /** 140 * Gets the time the cookie expires. 141 * 142 * @return int 143 */ 144 public function getExpiresTime() 145 { 146 return $this->expire; 147 } 148 149 /** 150 * Gets the path on the server in which the cookie will be available on. 151 * 152 * @return string 153 */ 154 public function getPath() 155 { 156 return $this->path; 157 } 158 159 /** 160 * Checks whether the cookie should only be transmitted over a secure HTTPS connection from the client. 161 * 162 * @return bool 163 */ 164 public function isSecure() 165 { 166 return $this->secure; 167 } 168 169 /** 170 * Checks whether the cookie will be made accessible only through the HTTP protocol. 171 * 172 * @return bool 173 */ 174 public function isHttpOnly() 175 { 176 return $this->httpOnly; 177 } 178 179 /** 180 * Whether this cookie is about to be cleared. 181 * 182 * @return bool 183 */ 184 public function isCleared() 185 { 186 return 0 !== $this->expire && $this->expire < time(); 187 } 188 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Wed Nov 11 20:33:01 2020 | Cross-referenced by PHPXref 0.7.1 |