Home > PHP Framework > YII > body text

How to check cookies in yii2

(*-*)浩
Release: 2019-12-30 11:49:17
Original
2129 people have browsed it

How to check cookies in yii2

Set Cookie

##PHP                                                                                                                                                       Tutorial)

Yii2

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

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"]
Copy after login

Yii2

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

Check Cookie

PHP

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

Yii2

$cookies = Yii::$app->request->cookies;
 
if (isset($cookies['user'])){ }
if ($cookies->has('user')){ }
if (isset($cookies['user'])){ }
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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 [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!