Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Static Public Member Functions | Protected Attributes
Zend_Http_Header_SetCookie Class Reference

Public Member Functions

 __construct ($name=null, $value=null, $expires=null, $path=null, $domain=null, $secure=false, $httponly=false, $maxAge=null, $version=null)
 
 getFieldName ()
 
 getFieldValue ()
 
 setName ($name)
 
 getName ()
 
 setValue ($value)
 
 getValue ()
 
 setVersion ($version)
 
 getVersion ()
 
 setMaxAge ($maxAge)
 
 getMaxAge ()
 
 setExpires ($expires)
 
 getExpires ($inSeconds=false)
 
 setDomain ($domain)
 
 getDomain ()
 
 setPath ($path)
 
 getPath ()
 
 setSecure ($secure)
 
 isSecure ()
 
 setHttponly ($httponly)
 
 isHttponly ()
 
 isExpired ($now=null)
 
 isSessionCookie ()
 
 isValidForRequest ($requestDomain, $path, $isSecure=false)
 
 toString ()
 
 __toString ()
 
 toStringMultipleHeaders (array $headers)
 

Static Public Member Functions

static fromString ($headerLine, $bypassHeaderFieldName=false)
 

Protected Attributes

 $name = null
 
 $value = null
 
 $version = null
 
 $maxAge = null
 
 $expires = null
 
 $domain = null
 
 $path = null
 
 $secure = null
 
 $httponly = null
 

Detailed Description

Definition at line 52 of file SetCookie.php.

Constructor & Destructor Documentation

◆ __construct()

__construct (   $name = null,
  $value = null,
  $expires = null,
  $path = null,
  $domain = null,
  $secure = false,
  $httponly = false,
  $maxAge = null,
  $version = null 
)

Cookie object constructor

Todo:
Add validation of each one of the parameters (legal domain, etc.)
Parameters
string$name
string$value
int$expires
string$path
string$domain
bool$secure
bool$httponly
string$maxAge
int$version
Returns
SetCookie

Definition at line 189 of file SetCookie.php.

190  {
191  $this->type = 'Cookie';
192 
193  if ($name) {
194  $this->setName($name);
195  }
196 
197  if ($value) {
198  $this->setValue($value); // in parent
199  }
200 
201  if ($version) {
202  $this->setVersion($version);
203  }
204 
205  if ($maxAge) {
206  $this->setMaxAge($maxAge);
207  }
208 
209  if ($domain) {
210  $this->setDomain($domain);
211  }
212 
213  if ($expires) {
214  $this->setExpires($expires);
215  }
216 
217  if ($path) {
218  $this->setPath($path);
219  }
220 
221  if ($secure) {
222  $this->setSecure($secure);
223  }
224 
225  if ($httponly) {
226  $this->setHttponly($httponly);
227  }
228  }

Member Function Documentation

◆ __toString()

__toString ( )

Definition at line 533 of file SetCookie.php.

534  {
535  return $this->toString();
536  }

◆ fromString()

static fromString (   $headerLine,
  $bypassHeaderFieldName = false 
)
static

Generate a new Cookie object from a cookie string (for example the value of the Set-Cookie HTTP header)

Exceptions
Zend_Http_Header_Exception_InvalidArgumentException
Parameters
$headerLine
bool$bypassHeaderFieldName
Returns
array|SetCookie

Definition at line 126 of file SetCookie.php.

127  {
128  list($name, $value) = explode(': ', $headerLine, 2);
129 
130  // check to ensure proper header type for this factory
131  if (strtolower($name) !== 'set-cookie') {
132  throw new Zend_Http_Header_Exception_InvalidArgumentException('Invalid header line for Set-Cookie string: "' . $name . '"');
133  }
134 
135  $multipleHeaders = preg_split('#(?<!Sun|Mon|Tue|Wed|Thu|Fri|Sat),\s*#', $value);
136  $headers = array();
137  foreach ($multipleHeaders as $headerLine) {
138  $header = new self();
139  $keyValuePairs = preg_split('#;\s*#', $headerLine);
140  foreach ($keyValuePairs as $keyValue) {
141  if (strpos($keyValue, '=')) {
142  list($headerKey, $headerValue) = preg_split('#=\s*#', $keyValue, 2);
143  } else {
144  $headerKey = $keyValue;
145  $headerValue = null;
146  }
147 
148  // First K=V pair is always the cookie name and value
149  if ($header->getName() === NULL) {
150  $header->setName($headerKey);
151  $header->setValue($headerValue);
152  continue;
153  }
154 
155  // Process the remanining elements
156  switch (str_replace(array('-', '_'), '', strtolower($headerKey))) {
157  case 'expires' : $header->setExpires($headerValue); break;
158  case 'domain' : $header->setDomain($headerValue); break;
159  case 'path' : $header->setPath($headerValue); break;
160  case 'secure' : $header->setSecure(true); break;
161  case 'httponly': $header->setHttponly(true); break;
162  case 'version' : $header->setVersion((int) $headerValue); break;
163  case 'maxage' : $header->setMaxAge((int) $headerValue); break;
164  default:
165  // Intentionally omitted
166  }
167  }
168  $headers[] = $header;
169  }
170  return count($headers) == 1 ? array_pop($headers) : $headers;
171  }

◆ getDomain()

getDomain ( )
Returns
string

Definition at line 422 of file SetCookie.php.

423  {
424  return $this->domain;
425  }

◆ getExpires()

getExpires (   $inSeconds = false)
Returns
int

Definition at line 398 of file SetCookie.php.

399  {
400  if ($this->expires == null) {
401  return;
402  }
403  if ($inSeconds) {
404  return $this->expires;
405  }
406  return gmdate('D, d-M-Y H:i:s', $this->expires) . ' GMT';
407  }

◆ getFieldName()

getFieldName ( )
Returns
string 'Set-Cookie'

Definition at line 233 of file SetCookie.php.

234  {
235  return 'Set-Cookie';
236  }

◆ getFieldValue()

getFieldValue ( )
Exceptions
Zend_Http_Header_Exception_RuntimeException
Returns
string

Definition at line 242 of file SetCookie.php.

243  {
244  if ($this->getName() == '') {
245  throw new Zend_Http_Header_Exception_RuntimeException('A cookie name is required to generate a field value for this cookie');
246  }
247 
248  $value = $this->getValue();
249  if (strpos($value,'"')!==false) {
250  $value = '"'.urlencode(str_replace('"', '', $value)).'"';
251  } else {
252  $value = urlencode($value);
253  }
254  $fieldValue = $this->getName() . '=' . $value;
255 
256  $version = $this->getVersion();
257  if ($version!==null) {
258  $fieldValue .= '; Version=' . $version;
259  }
260 
261  $maxAge = $this->getMaxAge();
262  if ($maxAge!==null) {
263  $fieldValue .= '; Max-Age=' . $maxAge;
264  }
265 
266  $expires = $this->getExpires();
267  if ($expires) {
268  $fieldValue .= '; Expires=' . $expires;
269  }
270 
271  $domain = $this->getDomain();
272  if ($domain) {
273  $fieldValue .= '; Domain=' . $domain;
274  }
275 
276  $path = $this->getPath();
277  if ($path) {
278  $fieldValue .= '; Path=' . $path;
279  }
280 
281  if ($this->isSecure()) {
282  $fieldValue .= '; Secure';
283  }
284 
285  if ($this->isHttponly()) {
286  $fieldValue .= '; HttpOnly';
287  }
288 
289  return $fieldValue;
290  }

◆ getMaxAge()

getMaxAge ( )

Get Max-Age

Returns
integer

Definition at line 373 of file SetCookie.php.

374  {
375  return $this->maxAge;
376  }

◆ getName()

getName ( )
Returns
string

Definition at line 309 of file SetCookie.php.

310  {
311  return $this->name;
312  }

◆ getPath()

getPath ( )
Returns
string

Definition at line 440 of file SetCookie.php.

441  {
442  return $this->path;
443  }

◆ getValue()

getValue ( )
Returns
string

Definition at line 327 of file SetCookie.php.

328  {
329  return $this->value;
330  }

◆ getVersion()

getVersion ( )

Get version

Returns
integer

Definition at line 350 of file SetCookie.php.

351  {
352  return $this->version;
353  }

◆ isExpired()

isExpired (   $now = null)

Check whether the cookie has expired

Always returns false if the cookie is a session cookie (has no expiry time)

Parameters
int$nowTimestamp to consider as "now"
Returns
boolean

Definition at line 487 of file SetCookie.php.

488  {
489  if ($now === null) {
490  $now = time();
491  }
492 
493  if (is_int($this->expires) && $this->expires < $now) {
494  return true;
495  } else {
496  return false;
497  }
498  }

◆ isHttponly()

isHttponly ( )
Returns
bool

Definition at line 474 of file SetCookie.php.

475  {
476  return $this->httponly;
477  }

◆ isSecure()

isSecure ( )
Returns
boolean

Definition at line 457 of file SetCookie.php.

458  {
459  return $this->secure;
460  }

◆ isSessionCookie()

isSessionCookie ( )

Check whether the cookie is a session cookie (has no expiry time set)

Returns
boolean

Definition at line 505 of file SetCookie.php.

506  {
507  return ($this->expires === null);
508  }

◆ isValidForRequest()

isValidForRequest (   $requestDomain,
  $path,
  $isSecure = false 
)

Definition at line 510 of file SetCookie.php.

511  {
512  if ($this->getDomain() && (strrpos($requestDomain, $this->getDomain()) !== false)) {
513  return false;
514  }
515 
516  if ($this->getPath() && (strpos($path, $this->getPath()) !== 0)) {
517  return false;
518  }
519 
520  if ($this->secure && $this->isSecure()!==$isSecure) {
521  return false;
522  }
523 
524  return true;
525 
526  }

◆ setDomain()

setDomain (   $domain)
Parameters
string$domain

Definition at line 412 of file SetCookie.php.

413  {
415  $this->domain = $domain;
416  return $this;
417  }

◆ setExpires()

setExpires (   $expires)
Parameters
int$expires
Returns
SetCookie

Definition at line 382 of file SetCookie.php.

383  {
384  if (!empty($expires)) {
385  if (is_string($expires)) {
386  $expires = strtotime($expires);
387  } elseif (!is_int($expires)) {
388  throw new Zend_Http_Header_Exception_InvalidArgumentException('Invalid expires time specified');
389  }
390  $this->expires = (int) $expires;
391  }
392  return $this;
393  }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17

◆ setHttponly()

setHttponly (   $httponly)
Parameters
bool$httponly

Definition at line 465 of file SetCookie.php.

466  {
467  $this->httponly = $httponly;
468  return $this;
469  }

◆ setMaxAge()

setMaxAge (   $maxAge)

Set Max-Age

Parameters
integer$maxAge

Definition at line 360 of file SetCookie.php.

361  {
362  if (!is_int($maxAge) || ($maxAge<0)) {
363  throw new Zend_Http_Header_Exception_InvalidArgumentException('Invalid Max-Age number specified');
364  }
365  $this->maxAge = $maxAge;
366  }

◆ setName()

setName (   $name)
Parameters
string$name
Returns
SetCookie

Definition at line 296 of file SetCookie.php.

297  {
298  if (preg_match("/[=,; \t\r\n\013\014]/", $name)) {
299  throw new Zend_Http_Header_Exception_InvalidArgumentException("Cookie name cannot contain these characters: =,; \\t\\r\\n\\013\\014 ({$name})");
300  }
301 
302  $this->name = $name;
303  return $this;
304  }

◆ setPath()

setPath (   $path)
Parameters
string$path

Definition at line 430 of file SetCookie.php.

431  {
433  $this->path = $path;
434  return $this;
435  }

◆ setSecure()

setSecure (   $secure)
Parameters
boolean$secure

Definition at line 448 of file SetCookie.php.

449  {
450  $this->secure = $secure;
451  return $this;
452  }

◆ setValue()

setValue (   $value)
Parameters
string$value

Definition at line 317 of file SetCookie.php.

318  {
320  $this->value = $value;
321  return $this;
322  }
$block setTitle( 'CMS Block Title') -> setIdentifier('fixture_block') ->setContent('< h1 >Fixture Block Title</h1 >< a href=" store url</a><p> Config value
Definition: block.php:9

◆ setVersion()

setVersion (   $version)

Set version

Parameters
integer$version

Definition at line 337 of file SetCookie.php.

338  {
339  if (!is_int($version)) {
340  throw new Zend_Http_Header_Exception_InvalidArgumentException('Invalid Version number specified');
341  }
342  $this->version = $version;
343  }

◆ toString()

toString ( )

Definition at line 528 of file SetCookie.php.

529  {
530  return $this->getFieldName() . ': ' . $this->getFieldValue();
531  }

◆ toStringMultipleHeaders()

toStringMultipleHeaders ( array  $headers)

Definition at line 538 of file SetCookie.php.

539  {
540  $headerLine = $this->toString();
541  /* @var $header SetCookie */
542  foreach ($headers as $header) {
543  if (!$header instanceof Zend_Http_Header_SetCookie) {
545  'The SetCookie multiple header implementation can only accept an array of SetCookie headers'
546  );
547  }
548  $headerLine .= ', ' . $header->getFieldValue();
549  }
550  return $headerLine;
551  }

Field Documentation

◆ $domain

$domain = null
protected

Definition at line 95 of file SetCookie.php.

◆ $expires

$expires = null
protected

Definition at line 88 of file SetCookie.php.

◆ $httponly

$httponly = null
protected

Definition at line 114 of file SetCookie.php.

◆ $maxAge

$maxAge = null
protected

Definition at line 81 of file SetCookie.php.

◆ $name

$name = null
protected

Definition at line 60 of file SetCookie.php.

◆ $path

$path = null
protected

Definition at line 102 of file SetCookie.php.

◆ $secure

$secure = null
protected

Definition at line 109 of file SetCookie.php.

◆ $value

$value = null
protected

Definition at line 67 of file SetCookie.php.

◆ $version

$version = null
protected

Definition at line 74 of file SetCookie.php.


The documentation for this class was generated from the following file: