首页 > 其他 > 详细

yii-cookie管理

时间:2014-02-12 01:28:04      阅读:449      评论:0      收藏:0      [点我收藏+]

一,操作

//设置
$cookie = new CHttpCookie(‘mycookie‘,‘this is my cookie‘);
$cookie->expire = time()+60*60*24*30;  //有限期30天
Yii::app()->request->cookies[‘mycookie‘]=$cookie;
//获取
$cookie = Yii::app()->request->getCookies();
$test_cookie =  $cookie[‘mycookie‘]->value;
//删除
unset($cookie[‘mycookie‘]);


二,注册

   CApplication::registerCoreComponents()

   

protected function registerCoreComponents()
{
    $components=array(
        ‘request‘=>array(
            ‘class‘=>‘CHttpRequest‘,
        ),
    );
    $this->setComponents($components);
}


三,源码

   CHttpRequest::getCookies();

class CCookieCollection extends CMap{ 
    protected function getCookies(){
        $cookies=array();
        if($this->_request->enableCookieValidation){  
            $sm=Yii::app()->getSecurityManager();    //获得安全管理器组件对象
            //遍历cookie数据
            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;
        }
    //...
}

   CMap

class CMap extends CComponent implements IteratorAggregate,ArrayAccess,Countable{
    public function offsetGet($offset){
        return $this->itemAt($offset);
    }
    public function offsetSet($offset,$item){
    $this->add($offset,$item);
    }
    public function offsetUnset($offset){
    $this->remove($offset);
    }
    //....
}






yii-cookie管理

原文:http://liyongjiang.blog.51cto.com/5800344/1358143

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!