2 回答

TA貢獻1911條經驗 獲得超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貢獻1868條經驗 獲得超4個贊
設置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 關注
- 885 瀏覽
添加回答
舉報