cookie operations
Set cookie
1
2
3
4
5
6
|
//获取response->cookies组件
$cookies
= \Yii::
$app
->response->cookies;
//设置cookie数组
$array_cookies
=
array
(
'name'
=>
'user'
,
'value'
=>
'doubly'
);
//添加cookie数据
$cookies
->add(
new
Cookie(
$array_cookies
));
|
Delete cookie
1
|
$cookies
->remove(
'user'
);
|
Read cookie
1
2
|
$cookies
= \Yii::
$app
->request->cookies;
echo
$cookies
->getValue(
"user"
);
|
Careful people may find and read cookie in front of the assembly is slightly different, in front of all response->cookies
, while the reading has spent request->cookies
, and this is why? Can refer to the previous article by Yii framework response saved cookie, read cookie with a request of principle
Operating session
In Yii framework for the operation of the session, we are told something by a session component, we first need to get the assembly session.
1
|
$session
= \Yii::
$app
->session;
|
After obtaining the session component you want to operate on session, we first need to determine whether to enable session
1
2
3
4
5
|
if
(
$session
->isActive){
echo
"session已经开启"
;
}
else
{
$session
->open();
}
|
After the session has been opened, we can set and use the session data, first data session is set
1
|
$session
->set(
"user"
,
"张三"
);
|
Read session data is simple
1
|
$user
=
$session
->get(
"user"
);
|
Similarly, there are deleted session data
1
|
$session
->remove(
"user"
);
|
In addition, we can also be operated session as an array
1
2
3
4
5
6
|
//设置session
$session
[
'user'
] =
'张三'
;
//读取session
$user
=
$session
[
'user'
];
//删除session
unset(
$session
[
'user'
]);
|