COOKIE and SESSION usage in Yii2.0

高洛峰
Release: 2023-03-05 13:28:02
Original
1651 people have browsed it

1. Cookie

Yii2 Cookies are mainly operated through yii/web/Request and yii/web/Response, through /Yii::$app->response->getCookies() ->add() adds Cookie and reads Cookie through /Yii::$app->request->cookies.

1) Add a Cookie

<?php
//第一种方法
$cookie = new /yii/web/Cookie();
$cookie -> name = &#39;smister&#39;;//cookie的名称
$cookie -> expire = time() + 3600; //存活的时间
$cookie -> httpOnly = true; //无法通过js读取cookie
$cookie -> value = &#39;cookieValue&#39;; //cookie的值
/Yii::$app->response->getCookies()->add($cookie);
//第二种方法
$cookie = new /yii/web/Cookie([
‘name&#39; => ‘smister&#39;,
‘expire&#39; => time() + 3600,
‘httpOnly &#39; => true,
‘value&#39; => ‘cookieValue&#39;
]);
/Yii::$app->response->getCookies()->add($cookie);
?>
Copy after login

2) Read a Cookie

<?php
$cookie = /Yii::$app->request->cookies;
//返回一个/yii/web/Cookie对象
$cookie->get(‘smister&#39;);
//直接返回Cookie的值
$cookie->getValue(‘smister&#39;); //$cookie[‘smister&#39;] 其实这样也是可以读取的
//判断一个Cookie是否存在
$cookie->has(‘smister&#39;);
//读取Cookie的总数
$cookie->count();//$cookie->getCount();跟count一样
?>
Copy after login

3) Delete Cookie

<?php
$cookie = Yii::$app->request->cookies->get(‘smister&#39;);
//移除一个Cookie对象
/Yii::$app->response->getCookies()->remove($cookie);
//移除所有Cookie,目前好像不太好使
/Yii::$app->response->getCookies()->removeAll();
?>
Copy after login

4) Note

Perform Cookie The response is called when adding, deleting or modifying. When reading cookies, Request

##2 and Session

are used. Yii2's Session is relatively simple and can be performed directly through /Yii::$app->session. Just do it

1) Add a session

<?php
$session = /Yii::$app->session;
$session->set(&#39;smister_name&#39; , &#39;myname&#39;);
$session->set(&#39;smister_array&#39; ,[1,2,3]);
?>
Copy after login

2) Read a session

<?php
$session = /Yii::$app->session;
//读取一个Session
$session->get(&#39;smister_name);
?>
Copy after login

3) Delete Session

<?php
$session = /Yii::$app->session;
//删除一个session
$session->remove(‘smister_name&#39;);
//删除所有session
$session->removeAll();
?>
Copy after login

The above is the usage of COOKIE and SESSION in Yii2.0 introduced by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message. , the editor will reply to everyone in time. I would also like to thank you all for your support of the PHP Chinese website!

For more articles related to COOKIE and SESSION usage in Yii2.0, please pay attention to 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 admin@php.cn
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!