2 回答
TA贡献1829条经验 获得超7个赞
$cookies = Yii::$app->response->cookies;
// add a new cookie to the response to be sent
$cookies->add(new \yii\web\Cookie([
'name' => 'username',
'value' => 'yiiuser',
]));
获取 Cookies
$cookies = Yii::$app->request->cookies;
// get the cookie value
$username = $cookies->getValue('username');
//return default value if the cookie is not available
$username = $cookies->getValue('username', 'default');
// Check the availability of the cookie
if ($cookies->has('username'))
echo $cookies->getValue('username');
删除 Cookies
$cookies = Yii::$app->response->cookies;
$cookies->remove('username');
unset($cookies['username']);
TA贡献1802条经验 获得超5个赞
设置cookie:
$cookie = new CHttpCookie('mycookie','this is my cookie');
$cookie->expire = time()+60*60*24*30; //有限期30天
Yii::app()->request->cookies['mycookie']=$cookie;
读取cookie:
$cookie = Yii::app()->request->getCookies();
echo $cookie['mycookie']->value;
销毁cookie:
$cookie = Yii::app()->request->getCookies();
unset($cookie[$name]);
- 2 回答
- 0 关注
- 778 浏览
添加回答
举报