个性化阅读
专注于IT技术分析

YII cookie介绍和用法示例

本文概述

Cookie是服务器嵌入用户系统中的一个小文件, 用于识别用户。

在Yii中, 每个cookie都是yii \ web \ Cookie的对象。

yii \ web \ Request(请求中提交的cookie的集合)和yii \ web \ Response(需要发送给用户的cookie的集合)通过名为cookies的属性维护cookie的集合。

控制器在应用程序中处理cookie请求和响应。因此, 应在控制器中读取并发送cookie。


设置 cookie

使用以下代码将Cookie发送给最终用户。

// get the cookie collection (yii\web\CookieCollection) from the "response" component
$cookies = Yii::$app->response->cookies;

// add a new cookie to the response to be sent
$cookies->add(new \yii\web\Cookie([
    'name' => 'name', 'value' => 'sssit', ]));

获取 cookie

要获取Cookie, 请使用以下代码。

// get the cookie collection (yii\web\CookieCollection) from the "request" component
$cookies = Yii::$app->request->cookies;

// get the cookie value. If the cookie does not exist, return "default" as the default value.
$name = $cookies->getValue('name', 'default');

// an alternative way of getting the "name" cookie value
if (($cookie = $cookies->get('name')) !== null) {
    $name = $cookie->value;
}

// you may also use $cookies like an array
if (isset($cookies['name'])) {
    $name = $cookies['name']->value;
}

// check if there is a "name" cookie
if ($cookies->has('name')) ...
if (isset($cookies['name'])) ...

删除 cookie

要删除cookie, 请使用Yii的remove()函数。

$cookies = Yii::$app->response->cookies;
// remove a cookie
$cookies->remove('name');
// equivalent to the following
unset($cookies['name']);

例:

让我们看一个设置和显示cookie值的示例。

步骤1在SiteController.php文件中添加两个动作actionSetCookie和actionShowCookie。

class SiteController extends Controller 
{ 
    /** 
     * @inheritdoc 
     */ 
    public function behaviors() 
    { 
        return [ 
            'access' => [ 
                'class' => AccessControl::className(), 'only' => ['logout', 'signup'], 'rules' => [ 
                    [ 
                        'actions' => ['signup'], 'allow' => true, 'roles' => ['?'], ], [ 
                        'actions' => ['logout', 'set-cookie', 'show-cookie'], 'allow' => true, 'roles' => ['@'], ], ], ], 'verbs' => [ 
                'class' => VerbFilter::className(), 'actions' => [ 
                    'logout' => ['post'], ], ], ]; 
    } 

    /** 
     * @inheritdoc 
     */ 
    public function actions() 
   { 
        return [ 
            'error' => [ 
                'class' => 'yii\web\ErrorAction', ], 'captcha' => [ 
                'class' => 'yii\captcha\CaptchaAction', 'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null, ], ]; 
    } 

    public function actionSetCookie() 
    { 
        $cookies = Yii::$app->response->cookies; 
        $cookies->add(new \yii\web\Cookie 
            ([ 
            'name' => 'test', 'value' => 'SSSIT Pvt. Ltd.' 
            ])); 
    } 

    public function actionShowCookie() 
    { 
        if(Yii::$app->getRequest()->getCookies()->has('test')) 
        { 
            print_r(Yii::$app->getRequest()->getCookies()->getValue('test')); 
        } 
    }

步骤2在浏览器上运行它, 以首先使用以下URL设置cookie,

http://localhost/cook/frontend/web/index.php?r = site / set-cookie

YII cookie1

步骤3在浏览器上运行它, 以显示具有以下URL的cookie,

http://localhost/cook/frontend/web/index.php?r = site / show-cookie

YII cookie2
赞(0)
未经允许不得转载:srcmini » YII cookie介绍和用法示例

评论 抢沙发

评论前必须登录!