The use of session and cookie in yii2.0 framework

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.