• yii2.0框架中session与cookie的用法


    我们在开发项目中南面使用到session给和cookie,那么在yii中有他自己的规则
    如下案例:
     

    session使用
        function actionS1(){
            echo $this->id."
    ";
            echo $this->action->id."
    ";
            设置session,通过session组件来设置
            Yii::app()->session['username'] = "zhangsan";
            Yii::app()->session['useraddr'] = "beijing";
            echo "make session success";
        }
       
        function actionS2(){
            使用session
            echo Yii::app()->session['username'],"
    ";
            echo Yii::app()->session['useraddr'];
            echo "use session success";
        }
       
        function actionS3(){
            删除一个session
            unset(Yii::app()->session['useraddr']);
           
            删除全部session
            Yii::app()->session->clear();  //删除session变量
            Yii::app()->session->destroy(); //删除服务器的session信息
        }
       
       cookie在Yii框架使用
        function actionC1(){
            设置cookie
            $ck = new CHttpCookie('hobby','篮球,足球');
            $ck -> expire = time()+3600;
            把$ck对象放入cookie组件里边
            Yii::app()->request->cookies['hobby'] = $ck;
           
            $ck2 = new CHttpCookie('sex','nan');
            $ck2 -> expire = time()+3600;
            把$ck对象放入cookie组件里边
            Yii::app()->request->cookies['sex'] = $ck2;
           
            echo "cookie make success";
        }
        function actionC2(){
            访问cookie
            echo Yii::app()->request->cookies['hobby'],"
    ";
            echo Yii::app()->request->cookies['sex'];
        }
        function actionC3(){
            删除cookie
            unset(Yii::app()->request->cookies['sex']);
        }
  • 相关阅读:
    中文分词技术
    布隆过滤器(Bloom Filter)详解
    真正理解Mysql的四种隔离级别
    从内核文件系统看文件读写过程
    如何评价腾讯开源的消息中间件TubeMQ?
    零拷贝(Zero-copy)及其应用详解
    深入了解RabbitMQ
    (四)下载利器aria2
    (三)轻量级文件服务器filebrowser
    log4j 2整理
  • 原文地址:https://www.cnblogs.com/bjfy/p/5556079.html
Copyright © 2020-2023  润新知