YII学习总结4(cookie操作)

cookie操作

<?php
    namespace app\controllers;
    use yii\web\Controller;
    use yii\web\Cookie;

    class HelloController extends Controller{
        //存入cookie和修改cookie
        $cookies = \YII::$app->response->cookies;        
        $cookie_data = array('name'=>"user","value"=>"zhangsan");
        $cookies->add(new Cookie($cookie_data));//以'cookieValidationKey' 为基础的加密字符串
        $cookies->remove("id");//移除掉ID的cookie



        //取出cookie的值
        $cookies = \YII::$app->request->cookies;
        $cookies->getValue("user");
        //如果cookie不存在
        $cookies->getValue("user",20);//返回20


    }
?>

 

posted @ 2016-05-17 17:19  雨落知音  阅读(194)  评论(0编辑  收藏  举报