Home >PHP Framework >YII >How to check cookies in yii2
Set Cookie
##PHP Tutorial)setcookie("name", "Larry", time()+3600);
$cookies = Yii::$app->response->cookies;
$cookies->add(new \yii\web\Cookie([
'name' => 'name',
'value' => 'Larry',
'expire'=>time()+3600
]));
expire This is a UNIX timestamp. If set to 0, or omitted, the cookie will disappear when the browser is closed
Get CookiePHP
$name=$_COOKIE["user"]
$cookies = Yii::$app->request->cookies;//注意此处是request
$language = $cookies->get('user', 'defaultName');//获取默认值
PHP
if(isset($_COOKIE["user"])){ }
$cookies = Yii::$app->request->cookies;
if (isset($cookies['user'])){ }
if ($cookies->has('user')){ }
if (isset($cookies['user'])){ }
The above is the detailed content of How to check cookies in yii2. For more information, please follow other related articles on the PHP Chinese website!