Home  >  Article  >  PHP Framework  >  How to check cookies in yii2

How to check cookies in yii2

(*-*)浩
(*-*)浩Original
2019-12-30 11:49:172310browse

How to check cookies in yii2

Set Cookie

##PHP                                                                                                                                                       Tutorial)

setcookie("name", "Larry", time()+3600);

Yii2

$cookies = Yii::$app->response->cookies;
 
$cookies->add(new \yii\web\Cookie([
    'name' => 'name',
    'value' => 'Larry',
    'expire'=>time()+3600
]));

Create a cookie named "name", assign the value to "Larry", and specify the cookie for one hour Expire after

expire This is a UNIX timestamp. If set to 0, or omitted, the cookie will disappear when the browser is closed

Get Cookie

PHP

$name=$_COOKIE["user"]

Yii2

$cookies = Yii::$app->request->cookies;//注意此处是request
$language = $cookies->get('user', 'defaultName');//获取默认值

Check Cookie

PHP

if(isset($_COOKIE["user"])){ }

Yii2

$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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn