- Yii 教程
 - Yii - 主页
 - Yii - 概述
 - Yii - 安装
 - Yii - 创建页面
 - Yii - 应用程序结构
 - Yii - 入口脚本
 - Yii - 控制器
 - Yii - 使用控制器
 - Yii - 使用动作
 - Yii - 模型
 - Yii - 小部件
 - Yii - 模块
 - Yii - 视图
 - Yii - 布局
 - Yii - 资产
 - Yii - 资产转换
 - Yii - 扩展
 - Yii - 创建扩展
 - Yii - HTTP 请求
 - Yii - 响应
 - Yii - URL 格式
 - Yii - URL 路由
 - Yii - URL 规则
 - Yii - HTML 表单
 - Yii - 验证
 - Yii - 临时验证
 - Yii - AJAX 验证
 - Yii - 会话
 - Yii - 使用闪存数据
 - Yii - cookie
 - Yii - 使用 Cookie
 - Yii - 文件上传
 - Yii - 格式化
 - Yii - 分页
 - Yii - 排序
 - Yii - 属性
 - Yii - 数据提供者
 - Yii - 数据小部件
 - Yii - 列表视图小部件
 - Yii - GridView 小部件
 - Yii - 活动
 - Yii - 创建事件
 - Yii - Behave
 - Yii - 创建Behave
 - Yii - 配置
 - Yii - 依赖注入
 - Yii - 数据库访问
 - Yii - 数据访问对象
 - Yii - 查询生成器
 - Yii - 活动记录
 - Yii - 数据库迁移
 - Yii - 主题化
 - Yii - RESTful API
 - Yii - RESTful API 的实际应用
 - Yii - 字段
 - Yii - 测试
 - Yii - 缓存
 - Yii - 片段缓存
 - Yii - 别名
 - Yii - 日志记录
 - Yii - 错误处理
 - Yii - 身份验证
 - Yii - 授权
 - Yii - 本地化
 - Yii-Gii
 - Gii – 创建模型
 - Gii – 生成控制器
 - Gii – 生成模块
 
- Yii 有用的资源
 - Yii - 快速指南
 - Yii - 有用的资源
 - Yii - 讨论
 
Yii - 使用 Cookie
Cookie 允许跨请求保存数据。在 PHP 中,您可以通过$_COOKIE变量访问它们。Yii 将 cookie 表示为yii\web\Cookie类的对象。在本章中,我们描述了几种读取cookie的方法。
步骤 1 -在SiteController中创建一个actionReadCookies方法。
public function actionReadCookies() { 
   // get cookies from the "request" component 
   $cookies = Yii::$app->request->cookies; 
   // get the "language" cookie value 
   // if the cookie does not exist, return "ru" as the default value 
   $language = $cookies->getValue('language', 'ru'); 
   // an alternative way of getting the "language" cookie value 
   if (($cookie = $cookies->get('language')) !== null) { 
      $language = $cookie->value; 
   } 
   // you may also use $cookies like an array 
   if (isset($cookies['language'])) { 
      $language = $cookies['language']->value; 
   } 
   // check if there is a "language" cookie 
   if ($cookies->has('language')) echo "Current language: $language"; 
}
步骤 2 - 要查看发送 cookie 的实际效果,请在SiteController中创建一个名为actionSendCookies的方法。
public function actionSendCookies() { 
   // get cookies 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' => 'language', 
      'value' => 'ru-RU', 
   ])); 
   $cookies->add(new \yii\web\Cookie([
      'name' => 'username', 
      'value' => 'John', 
   ])); 
   $cookies->add(new \yii\web\Cookie([ 
      'name' => 'country', 
      'value' => 'USA', 
   ])); 
} 
步骤 3 - 现在,如果您访问http://localhost:8080/index.php?r=site/send-cookies,您会注意到 cookie 保存在浏览器内。
在 Yii 中,默认情况下启用 cookie 验证。它可以保护 cookie 不被客户端修改。config/web.php 文件中的哈希字符串对每个 cookie 进行签名。
<?php 
   $params = require(__DIR__ . '/params.php'); 
   $config = [ 
      'id' => 'basic', 
      'basePath' => dirname(__DIR__), 
      'bootstrap' => ['log'], 
      'components' => [ 
         'request' => [ 
            // !!! insert a secret key in the following (if it is empty) - this is 
            //required by cookie validation 
            'cookieValidationKey' => 'ymoaYrebZHa8gURuolioHGlK8fLXCKjO', 
         ], 
         'cache' => [ 
            'class' => 'yii\caching\FileCache', 
         ], 
         'user' => [ 
            'identityClass' => 'app\models\User', 
            'enableAutoLogin' => true, 
         ], 
         'errorHandler' => [ 
            'errorAction' => 'site/error', 
         ], 
         'mailer' => [ 
            'class' => 'yii\swiftmailer\Mailer', 
            // send all mails to a file by default. You have to set 
            // 'useFileTransport' to false and configure a transport 
            // for the mailer to send real emails. 
            'useFileTransport' => true, 
         ], 
         'log' => [ 
            'traceLevel' => YII_DEBUG ? 3 : 0, 
            'targets' => [ 
               [ 
                  'class' => 'yii\log\FileTarget', 
                     'levels' => ['error', 'warning'], 
                ], 
            ], 
         ], 
         'urlManager' => [ 
            //'showScriptName' => false, 
            //'enablePrettyUrl' => true, 
            //'enableStrictParsing' => true, 
            //'suffix' => '/' 
         ], 
         'db' => require(__DIR__ . '/db.php'), 
      ], 
      'modules' => [ 
         'hello' => [ 
            'class' => 'app\modules\hello\Hello', 
         ], 
      ], 
      'params' => $params,
   ]; 
   if (YII_ENV_DEV) { 
      // configuration adjustments for 'dev' environment 
      $config['bootstrap'][] = 'debug'; 
      $config['modules']['debug'] = [ 
         'class' => 'yii\debug\Module', 
      ]; 
      $config['bootstrap'][] = 'gii'; 
      $config['modules']['gii'] = [ 
         'class' => 'yii\gii\Module', 
      ]; 
   } 
   return $config; 
?>
您可以通过将yii\web\Request::$enableCookieValidation属性设置为false来禁用 cookie 验证。