Yii2中cookie用法示例分析
來源:易賢網(wǎng) 閱讀:1023 次 日期:2016-08-12 16:13:29
溫馨提示:易賢網(wǎng)小編為您整理了“Yii2中cookie用法示例分析”,方便廣大網(wǎng)友查閱!

本文實例講述了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程序設計有所幫助。

更多信息請查看網(wǎng)絡編程
易賢網(wǎng)手機網(wǎng)站地址:Yii2中cookie用法示例分析
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 加入群交流 | 手機站點 | 投訴建議
工業(yè)和信息化部備案號:滇ICP備2023014141號-1 云南省教育廳備案號:云教ICP備0901021 滇公網(wǎng)安備53010202001879號 人力資源服務許可證:(云)人服證字(2023)第0102001523號
聯(lián)系電話:0871-65317125(9:00—18:00) 獲取招聘考試信息及咨詢關(guān)注公眾號:hfpxwx
咨詢QQ:526150442(9:00—18:00)版權(quán)所有:易賢網(wǎng)