Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Data Fields | Protected Member Functions
PhpCookieManager Class Reference
Inheritance diagram for PhpCookieManager:
CookieManagerInterface CookieReaderInterface CookieManager

Public Member Functions

 __construct (CookieScopeInterface $scope, CookieReaderInterface $reader, LoggerInterface $logger=null, HttpHeader $httpHeader=null)
 
 setSensitiveCookie ($name, $value, SensitiveCookieMetadata $metadata=null)
 
 setPublicCookie ($name, $value, PublicCookieMetadata $metadata=null)
 
 getCookie ($name, $default=null)
 
 deleteCookie ($name, CookieMetadata $metadata=null)
 

Data Fields

const MAX_NUM_COOKIES = 50
 
const MAX_COOKIE_SIZE = 4096
 
const EXPIRE_NOW_TIME = 1
 
const EXPIRE_AT_END_OF_SESSION_TIME = 0
 
const KEY_EXPIRE_TIME = 'expiry'
 

Protected Member Functions

 setCookie ($name, $value, array $metadataArray)
 

Detailed Description

CookieManager helps manage the setting, retrieving and deleting of cookies.

To aid in security, the cookie manager will make it possible for the application to indicate if the cookie contains sensitive data so that extra protection can be added to the contents of the cookie as well as how the browser stores the cookie.

@SuppressWarnings(PHPMD.CouplingBetweenObjects)

Definition at line 25 of file PhpCookieManager.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( CookieScopeInterface  $scope,
CookieReaderInterface  $reader,
LoggerInterface  $logger = null,
HttpHeader  $httpHeader = null 
)
Parameters
CookieScopeInterface$scope
CookieReaderInterface$reader
LoggerInterface$logger
HttpHeader$httpHeader

Definition at line 72 of file PhpCookieManager.php.

77  {
78  $this->scope = $scope;
79  $this->reader = $reader;
80  $this->logger = $logger ?: ObjectManager::getInstance()->get(LoggerInterface::class);
81  $this->httpHeader = $httpHeader ?: ObjectManager::getInstance()->get(HttpHeader::class);
82  }
$logger

Member Function Documentation

◆ deleteCookie()

deleteCookie (   $name,
CookieMetadata  $metadata = null 
)

Deletes a cookie with the given name.

Parameters
string$name
CookieMetadata$metadata
Returns
void
Exceptions
FailureToSendExceptionIf cookie couldn't be sent to the browser. If this exception isn't thrown, there is still no guarantee that the browser received and accepted the request to delete this cookie.
InputExceptionIf the cookie name is empty or contains invalid characters.

Implements CookieManagerInterface.

Definition at line 291 of file PhpCookieManager.php.

292  {
293  $metadataArray = $this->scope->getCookieMetadata($metadata)->__toArray();
294 
295  // explicitly set an expiration time in the metadataArray.
297 
298  $this->checkAbilityToSendCookie($name, '');
299 
300  // cookie value set to empty string to delete from the remote client
301  $this->setCookie($name, '', $metadataArray);
302 
303  // Remove the cookie
304  unset($_COOKIE[$name]);
305  }
if(!isset($_GET['name'])) $name
Definition: log.php:14

◆ getCookie()

getCookie (   $name,
  $default = null 
)

Retrieve a value from a cookie.

Parameters
string$name
string | null$defaultThe default value to return if no value could be found for the given $name.
Returns
string|null

Implements CookieReaderInterface.

Definition at line 275 of file PhpCookieManager.php.

276  {
277  return $this->reader->getCookie($name, $default);
278  }
if(!isset($_GET['name'])) $name
Definition: log.php:14

◆ setCookie()

setCookie (   $name,
  $value,
array  $metadataArray 
)
protected

Set a value in a cookie with the given $name $value pairing.

Parameters
string$name
string$value
array$metadataArray
Returns
void
Exceptions
FailureToSendExceptionIf cookie couldn't be sent to the browser.
CookieSizeLimitReachedExceptionThrown when the cookie is too big to store any additional data.
InputExceptionIf the cookie name is empty or contains invalid characters.

Definition at line 135 of file PhpCookieManager.php.

136  {
137  $expire = $this->computeExpirationTime($metadataArray);
138 
139  $this->checkAbilityToSendCookie($name, $value);
140 
141  $phpSetcookieSuccess = setcookie(
142  $name,
143  $value,
144  $expire,
145  $this->extractValue(CookieMetadata::KEY_PATH, $metadataArray, ''),
146  $this->extractValue(CookieMetadata::KEY_DOMAIN, $metadataArray, ''),
147  $this->extractValue(CookieMetadata::KEY_SECURE, $metadataArray, false),
148  $this->extractValue(CookieMetadata::KEY_HTTP_ONLY, $metadataArray, false)
149  );
150 
151  if (!$phpSetcookieSuccess) {
152  $params['name'] = $name;
153  if ($value == '') {
154  throw new FailureToSendException(
155  new Phrase('The cookie with "%name" cookieName couldn\'t be deleted.', $params)
156  );
157  } else {
158  throw new FailureToSendException(
159  new Phrase('The cookie with "%name" cookieName couldn\'t be sent. Please try again later.', $params)
160  );
161  }
162  }
163  }
$value
Definition: gender.phtml:16
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18
if(!isset($_GET['name'])) $name
Definition: log.php:14

◆ setPublicCookie()

setPublicCookie (   $name,
  $value,
PublicCookieMetadata  $metadata = null 
)

Set a value in a public cookie with the given $name $value pairing.

Public cookies can be accessed by JS. HttpOnly will be set to false by default for these cookies, but can be changed to true.

Parameters
string$name
string$value
PublicCookieMetadata$metadata
Returns
void
Exceptions
FailureToSendExceptionIf cookie couldn't be sent to the browser.
CookieSizeLimitReachedExceptionThrown when the cookie is too big to store any additional data.
InputExceptionIf the cookie name is empty or contains invalid characters.

Implements CookieManagerInterface.

Definition at line 118 of file PhpCookieManager.php.

119  {
120  $metadataArray = $this->scope->getPublicCookieMetadata($metadata)->__toArray();
121  $this->setCookie($name, $value, $metadataArray);
122  }
$value
Definition: gender.phtml:16
if(!isset($_GET['name'])) $name
Definition: log.php:14

◆ setSensitiveCookie()

setSensitiveCookie (   $name,
  $value,
SensitiveCookieMetadata  $metadata = null 
)

Set a value in a private cookie with the given $name $value pairing.

Sensitive cookies cannot be accessed by JS. HttpOnly will always be set to true for these cookies.

Parameters
string$name
string$value
SensitiveCookieMetadata$metadata
Returns
void
Exceptions
FailureToSendExceptionCookie couldn't be sent to the browser. If this exception isn't thrown, there is still no guarantee that the browser received and accepted the cookie.
CookieSizeLimitReachedExceptionThrown when the cookie is too big to store any additional data.
InputExceptionIf the cookie name is empty or contains invalid characters.

Implements CookieManagerInterface.

Definition at line 98 of file PhpCookieManager.php.

99  {
100  $metadataArray = $this->scope->getSensitiveCookieMetadata($metadata)->__toArray();
101  $this->setCookie($name, $value, $metadataArray);
102  }
$value
Definition: gender.phtml:16
if(!isset($_GET['name'])) $name
Definition: log.php:14

Field Documentation

◆ EXPIRE_AT_END_OF_SESSION_TIME

const EXPIRE_AT_END_OF_SESSION_TIME = 0

Definition at line 35 of file PhpCookieManager.php.

◆ EXPIRE_NOW_TIME

const EXPIRE_NOW_TIME = 1

Definition at line 34 of file PhpCookieManager.php.

◆ KEY_EXPIRE_TIME

const KEY_EXPIRE_TIME = 'expiry'

#- #+ Constant for metadata array key

Definition at line 41 of file PhpCookieManager.php.

◆ MAX_COOKIE_SIZE

const MAX_COOKIE_SIZE = 4096

Definition at line 33 of file PhpCookieManager.php.

◆ MAX_NUM_COOKIES

const MAX_NUM_COOKIES = 50

#+ Constants for Cookie manager. RFC 2109 - Page 15 http://www.ietf.org/rfc/rfc6265.txt

Definition at line 32 of file PhpCookieManager.php.


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