CCookieCollection
CCookieCollection implements a collection class to store cookies.
You normally access it via
CHttpRequest::getCookies().
Since CCookieCollection extends from
CMap, it can be used like an associative array as follows:
$cookies[$name]=new CHttpCookie($name,$value); // sends a cookie
$value=$cookies[$name]->value; // reads a cookie value
unset($cookies[$name]); // removes a cookie
Protected Properties
| Property |
Type |
Description |
Defined By |
| cookies | array | list of validated cookies | CCookieCollection |
Property Details
protected array getCookies()
list of validated cookies
public CHttpRequest getRequest()
the request instance
Method Details
Source Code: framework/web/CHttpRequest.php#1413 (
show)
public function __construct(CHttpRequest $request)
{
$this->_request=$request;
$this->copyfrom($this->getCookies());
$this->_initialized=true;
}
Constructor.
Source Code: framework/web/CHttpRequest.php#1459 (
show)
public function add($name,$cookie)
{
if($cookie instanceof CHttpCookie)
{
$this->remove($name);
parent::add($name,$cookie);
if($this->_initialized)
$this->addCookie($cookie);
}
else
throw new CException(Yii::t('yii','CHttpCookieCollection can only hold CHttpCookie objects.'));
}
Adds a cookie with the specified name. This overrides the parent implementation by performing additional operations for each newly added CHttpCookie object.
Source Code: framework/web/CHttpRequest.php#1508 (
show)
protected function addCookie($cookie)
{
$value=$cookie->value;
if($this->_request->enableCookieValidation)
$value=Yii::app()->getSecurityManager()->hashData(serialize($value));
if(version_compare(PHP_VERSION,'5.2.0','>='))
setcookie($cookie->name,$value,$cookie->expire,$cookie->path,$cookie->domain,$cookie->secure,$cookie->httpOnly);
else
setcookie($cookie->name,$value,$cookie->expire,$cookie->path,$cookie->domain,$cookie->secure);
}
Sends a cookie.
protected array getCookies() |
| {return} | array | list of validated cookies |
Source Code: framework/web/CHttpRequest.php#1431 (
show)
protected function getCookies()
{
$cookies=array();
if($this->_request->enableCookieValidation)
{
$sm=Yii::app()->getSecurityManager();
foreach($_COOKIE as $name=>$value)
{
if(is_string($value) && ($value=$sm->validateData($value))!==false)
$cookies[$name]=new CHttpCookie($name,@unserialize($value));
}
}
else
{
foreach($_COOKIE as $name=>$value)
$cookies[$name]=new CHttpCookie($name,$value);
}
return $cookies;
}
public CHttpCookie remove(mixed $name, array $options=array ( )) |
| $name | mixed | Cookie name. |
| $options | array | Cookie configuration array consisting of name-value pairs, available since 1.1.11. |
| {return} | CHttpCookie | The removed cookie object. |
Source Code: framework/web/CHttpRequest.php#1490 (
show)
public function remove($name,$options=array())
{
if(($cookie=parent::remove($name))!==null)
{
if($this->_initialized)
{
$cookie->configure($options);
$this->removeCookie($cookie);
}
}
return $cookie;
}
Removes a cookie with the specified name. This overrides the parent implementation by performing additional cleanup work when removing a CHttpCookie object. Since version 1.1.11, the second parameter is available that can be used to specify the options of the CHttpCookie being removed. For example, this may be useful when dealing with ".domain.tld" where multiple subdomains are expected to be able to manage cookies:
$options=array('domain'=>'.domain.tld');
Yii::app()->request->cookies['foo']=new CHttpCookie('cookie','value',$options);
Yii::app()->request->cookies->remove('cookie',$options);
Source Code: framework/web/CHttpRequest.php#1523 (
show)
protected function removeCookie($cookie)
{
if(version_compare(PHP_VERSION,'5.2.0','>='))
setcookie($cookie->name,'',0,$cookie->path,$cookie->domain,$cookie->secure,$cookie->httpOnly);
else
setcookie($cookie->name,'',0,$cookie->path,$cookie->domain,$cookie->secure);
}
Deletes a cookie.