CCookieCollection
包 | system.web |
---|---|
继承 | class CCookieCollection » CMap » CComponent |
实现 | Countable, ArrayAccess, Traversable, IteratorAggregate |
可用自 | 1.0 |
源码 | framework/web/CHttpRequest.php |
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 | Returns the number of items in the map. | CMap |
iterator | CMapIterator | Returns an iterator for traversing the items in the list. | CMap |
keys | array | the key list | CMap |
readOnly | boolean | whether this map is read-only or not. | CMap |
request | CHttpRequest | the request instance | CCookieCollection |
公共方法
方法 | 描述 | 被定义在 |
---|---|---|
__call() | Calls the named method which is not a class method. | CComponent |
__construct() | Constructor. | CCookieCollection |
__get() | Returns a property value, an event handler list or a behavior based on its name. | CComponent |
__isset() | Checks if a property value is null. | CComponent |
__set() | Sets value of a component property. | CComponent |
__unset() | Sets a component property to be null. | CComponent |
add() | Adds a cookie with the specified name. | CCookieCollection |
asa() | Returns the named behavior object. | CComponent |
attachBehavior() | Attaches a behavior to this component. | CComponent |
attachBehaviors() | Attaches a list of behaviors to the component. | CComponent |
attachEventHandler() | Attaches an event handler to an event. | CComponent |
canGetProperty() | Determines whether a property can be read. | CComponent |
canSetProperty() | Determines whether a property can be set. | CComponent |
clear() | Removes all items in the map. | CMap |
contains() | CMap | |
copyFrom() | Copies iterable data into the map. | CMap |
count() | Returns the number of items in the map. | CMap |
detachBehavior() | Detaches a behavior from the component. | CComponent |
detachBehaviors() | Detaches all behaviors from the component. | CComponent |
detachEventHandler() | Detaches an existing event handler. | CComponent |
disableBehavior() | Disables an attached behavior. | CComponent |
disableBehaviors() | Disables all behaviors attached to this component. | CComponent |
enableBehavior() | Enables an attached behavior. | CComponent |
enableBehaviors() | Enables all behaviors attached to this component. | CComponent |
evaluateExpression() | Evaluates a PHP expression or callback under the context of this component. | CComponent |
getCount() | Returns the number of items in the map. | CMap |
getEventHandlers() | Returns the list of attached event handlers for an event. | CComponent |
getIterator() | Returns an iterator for traversing the items in the list. | CMap |
getKeys() | Returns the key list | CMap |
getReadOnly() | Returns whether this map is read-only or not. Defaults to false. | CMap |
getRequest() | Returns the request instance | CCookieCollection |
hasEvent() | Determines whether an event is defined. | CComponent |
hasEventHandler() | Checks whether the named event has attached handlers. | CComponent |
hasProperty() | Determines whether a property is defined. | CComponent |
itemAt() | Returns the item with the specified key. | CMap |
mergeArray() | Merges two or more arrays into one recursively. | CMap |
mergeWith() | Merges iterable data into the map. | CMap |
offsetExists() | Returns whether there is an element at the specified offset. | CMap |
offsetGet() | Returns the element at the specified offset. | CMap |
offsetSet() | Sets the element at the specified offset. | CMap |
offsetUnset() | Unsets the element at the specified offset. | CMap |
raiseEvent() | Raises an event. | CComponent |
remove() | Removes a cookie with the specified name. | CCookieCollection |
toArray() | CMap |
受保护的方法
方法 | 描述 | 被定义在 |
---|---|---|
addCookie() | Sends a cookie. | CCookieCollection |
getCookies() | Returns list of validated cookies | CCookieCollection |
removeCookie() | Deletes a cookie. | CCookieCollection |
setReadOnly() | Sets whether this list is read-only or not | 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. |
源码: framework/web/CHttpRequest.php#1428 (显示)
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. |
源码: framework/web/CHttpRequest.php#1474 (显示)
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 |
源码: framework/web/CHttpRequest.php#1523 (显示)
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 |
源码: framework/web/CHttpRequest.php#1446 (显示)
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 |
remove()
方法
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. |
源码: framework/web/CHttpRequest.php#1505 (显示)
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);
removeCookie()
方法
protected void removeCookie(CHttpCookie $cookie)
| ||
$cookie | CHttpCookie | cookie to be deleted |
源码: framework/web/CHttpRequest.php#1538 (显示)
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.