简要咨询咨询QQ网站导航网站搜索手机站点联系我们设为首页加入收藏 

Yii2中cookie用法示例分析

来源:易贤网   阅读:1413 次  日期:2016-08-12 16:13:29

温馨提示:易贤网小编为您整理了“Yii2中cookie用法示例分析”,方便广大网友查阅!

本文实例讲述了Yii2中cookie用法。分享给大家供大家参考,具体如下:

<?php

//设置方法

$cookie = new Cookie([

  'name' => 'cookie_monster',

  'value' => 'Me want cookie!',

  'expire' => time() + 86400 * 365,

]);

\Yii::$app->getResponse()->getCookies()->add($cookie);

//读取方法

$value = \Yii::$app->getRequest()->getCookies()->getValue('my_cookie');

//给cookie加域名

$cookie = new Cookie([

  'name' => 'cookie_monster',

  'value' => 'Me want cookie everywhere!',

  'expire' => time() + 86400 * 365,

  'domain' => '.example.com' // <<<=== HERE

]);

\Yii::$app->getResponse()->getCookies()->add($cookie);

//设置登录cookie

$config = [

  // ...

  'components' => [

    // ...

    'user' => [

      'class' => 'yii\web\User',

      'identityClass' => 'app\models\User',

      'enableAutoLogin' => true,

      'loginUrl' => '/user/login',

      'identityCookie' => [ // <---- here!

        'name' => '_identity',

        'httpOnly' => true,

        'domain' => '.example.com',

      ],

    ],

    'request' => [

      'cookieValidationKey' => 'your_validation_key'

    ],

    'session' => [

      'cookieParams' => [

        'domain' => '.example.com',

        'httpOnly' => true,

      ],

    ],

  ],

];

//只给批定目录配置cookie

$config = [

  // ...

  'components' => [

    // ...

    'session' => [

      'name' => 'admin_session',

      'cookieParams' => [

        'httpOnly' => true,

        'path' => '/admin',

      ],

    ],

  ],

];

?>

希望本文所述对大家基于Yii框架的PHP程序设计有所帮助。

更多信息请查看网络编程
点此处就本文及相关问题在本站进行非正式的简要咨询(便捷快速)】     【点此处查询各地各类考试咨询QQ号码及交流群
上一篇:php 解决substr()截取中文字符乱码问题
下一篇:PHP socket 模拟POST 请求实例代码
易贤网手机网站地址:Yii2中cookie用法示例分析
由于各方面情况的不断调整与变化,易贤网提供的所有考试信息和咨询回复仅供参考,敬请考生以权威部门公布的正式信息和咨询为准!