We use the session and Cookie in the south of the development project, so there are rules in yii.
The following cases:
Session use
FunctionactionS1 () {
echo $this->id. "
";
echo $this->action->id. "
";
Set session, set by session component
Yii::app ()->session[' username '] = "Zhangsan";
Yii::app ()->session[' useraddr '] = "Beijing";
echo "Make session Success";
}
FunctionactionS2 () {
Use session
Echo Yii::app ()->session[' username '], "
";
Echo Yii::app ()->session[' useraddr '];
echo "Use session success";
}
FunctionactionS3 () {
Delete a session
Unset (Yii::app ()->session[' useraddr ']);
Delete all session
Yii::app ()->session->clear (); Delete Session variable
Yii::app ()->session->destroy (); Delete session information for the server
}
Cookies are used in YII framework
FunctionactionC1 () {
Set cookies
$ck = new Chttpcookie (' hobby ', ' basketball, football ');
$ck-expire = time () +3600;
Put the $ck object inside the cookie component
Yii::app ()->request->cookies[' hobby '] = $ck;
$ck 2 = new Chttpcookie (' Sex ', ' nan ');
$ck 2-expire = time () +3600;
Put the $ck object inside the cookie component
Yii::app ()->request->cookies[' sex ' = $ck 2;
echo "Cookie make success";
}
FunctionactionC2 () {
accessing cookies
Echo Yii::app ()->request->cookies[' hobby '], "
";
Echo Yii::app ()->request->cookies[' sex ';
}
FunctionactionC3 () {
Delete Cookies
Unset (Yii::app ()->request->cookies[' sex ');
}
The use of session and cookie in yii2.0 framework