【问题标题】:How to use CHttpCookie in Yii2Yii2中如何使用CHttpCookie
【发布时间】:2015-11-10 02:03:00
【问题描述】:

我有下面的代码

use yii\web\CHttpCookie;

$user_identifier = md5(rand()); 
$cookie = new CHttpCookie('user_identifier', $user_identifier);
$cookie->expire = time() + (60*60*24*365*5); 

但是输出错误,请告诉我如何使用HttpCookie

Class 'yii\web\CHttpCookie' not found

【问题讨论】:

    标签: cookies yii2 httpcookie


    【解决方案1】:

    Yii2 中没有 C 前缀的类。 Yii2 中的等价物是:

    // 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' => 'user_identifier',
        'value' => md5(rand()),
        'expire' => time() + 60 * 60 * 24 * 365 * 5,
    ]));
    
    // remove a cookie
    $cookies->remove('user_identifier');
    // equivalent to the following
    unset($cookies['user_identifier']);
    

    "Setting Cookies" 部分的"Setting Cookies" 部分的官方文档中对此进行了介绍。

    【讨论】:

      猜你喜欢
      • 2014-09-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多