最新消息: 新版网站上线了!!!

yii2使用cookie的方法

  1. 设置cookeies
  2. 获取Cookies
  3. 删除Cookies

设置Cookies

 


$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']);

转载请注明:谷谷点程序 » yii2使用cookie的方法