包 | system.web |
---|---|
继承 | class CCookieCollection » CMap » CComponent |
实现 | Countable, ArrayAccess, Traversable, IteratorAggregate |
源自 | 1.0 |
版本 | $Id: CHttpRequest.php 3560 2012-02-10 14:13:00Z mdomba $ |
源码 |
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:
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
公共属性
属性 | 类型 | 描述 | 定义在 |
---|---|---|---|
count | integer | 返回map中的项目数。 | CMap |
iterator | CMapIterator | 返回遍历这个列表的项目的迭代器。 | CMap |
keys | array | 返回键名列表 | CMap |
readOnly | boolean | 返回值说明这个列表是否为只读。默认为false。 | CMap |
request | CHttpRequest | the request instance | CCookieCollection |
受保护属性
属性 | 类型 | 描述 | 定义在 |
---|---|---|---|
cookies | array | list of validated cookies | CCookieCollection |
公共方法
受保护方法
方法 | 描述 | 定义在 |
---|---|---|
addCookie() | Sends a cookie. | CCookieCollection |
getCookies() | 返回list of validated cookies | CCookieCollection |
removeCookie() | Deletes a cookie. | CCookieCollection |
setReadOnly() | 设置设置这个列表是否为只读 | CMap |
属性详细
cookies
属性
只读
protected array getCookies()
list of validated cookies
request
属性
只读
public CHttpRequest getRequest()
the request instance
方法详细
__construct()
方法
public void __construct(CHttpRequest $request)
| ||
$request | CHttpRequest | owner of this collection. |
public function __construct(CHttpRequest $request)
{
$this->_request=$request;
$this->copyfrom($this->getCookies());
$this->_initialized=true;
}
Constructor.
add()
方法
public void add(mixed $name, CHttpCookie $cookie)
| ||
$name | mixed | Cookie name. |
$cookie | CHttpCookie | Cookie object. |
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.
addCookie()
方法
protected void addCookie(CHttpCookie $cookie)
| ||
$cookie | CHttpCookie | cookie to be sent |
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.
getCookies()
方法
protected array getCookies()
| ||
{return} | array | list of validated cookies |
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;
}
getRequest()
方法
public CHttpRequest getRequest()
| ||
{return} | CHttpRequest | the request instance |
public function getRequest()
{
return $this->_request;
}
remove()
方法
public CHttpCookie remove(mixed $name)
| ||
$name | mixed | Cookie name. |
{return} | CHttpCookie | The removed cookie object. |
public function remove($name)
{
if(($cookie=parent::remove($name))!==null)
{
if($this->_initialized)
$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.
removeCookie()
方法
protected void removeCookie(CHttpCookie $cookie)
| ||
$cookie | CHttpCookie | cookie to be deleted |
protected function removeCookie($cookie)
{
if(version_compare(PHP_VERSION,'5.2.0','>='))
setcookie($cookie->name,null,0,$cookie->path,$cookie->domain,$cookie->secure,$cookie->httpOnly);
else
setcookie($cookie->name,null,0,$cookie->path,$cookie->domain,$cookie->secure);
}
Deletes a cookie.