yii2.0框架中session与cookie的用法
Posted 不解凡忧
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了yii2.0框架中session与cookie的用法相关的知识,希望对你有一定的参考价值。
我们在开发项目中南面使用到session给和cookie,那么在yii中有他自己的规则
如下案例:
session使用
function actionS1(){
echo $this->id."
";
echo $this->action->id."
";
设置session,通过session组件来设置
Yii::app()->session[‘username‘] = "zhangsan";
Yii::app()->session[‘useraddr‘] = "beijing";
echo "make session success";
}
function actionS2(){
使用session
echo Yii::app()->session[‘username‘],"
";
echo Yii::app()->session[‘useraddr‘];
echo "use session success";
}
function actionS3(){
删除一个session
unset(Yii::app()->session[‘useraddr‘]);
删除全部session
Yii::app()->session->clear(); //删除session变量
Yii::app()->session->destroy(); //删除服务器的session信息
}
cookie在Yii框架使用
function actionC1(){
设置cookie
$ck = new CHttpCookie(‘hobby‘,‘篮球,足球‘);
$ck -> expire = time()+3600;
把$ck对象放入cookie组件里边
Yii::app()->request->cookies[‘hobby‘] = $ck;
$ck2 = new CHttpCookie(‘sex‘,‘nan‘);
$ck2 -> expire = time()+3600;
把$ck对象放入cookie组件里边
Yii::app()->request->cookies[‘sex‘] = $ck2;
echo "cookie make success";
}
function actionC2(){
访问cookie
echo Yii::app()->request->cookies[‘hobby‘],"
";
echo Yii::app()->request->cookies[‘sex‘];
}
function actionC3(){
删除cookie
unset(Yii::app()->request->cookies[‘sex‘]);
}
function actionS1(){
echo $this->id."
";
echo $this->action->id."
";
设置session,通过session组件来设置
Yii::app()->session[‘username‘] = "zhangsan";
Yii::app()->session[‘useraddr‘] = "beijing";
echo "make session success";
}
function actionS2(){
使用session
echo Yii::app()->session[‘username‘],"
";
echo Yii::app()->session[‘useraddr‘];
echo "use session success";
}
function actionS3(){
删除一个session
unset(Yii::app()->session[‘useraddr‘]);
删除全部session
Yii::app()->session->clear(); //删除session变量
Yii::app()->session->destroy(); //删除服务器的session信息
}
cookie在Yii框架使用
function actionC1(){
设置cookie
$ck = new CHttpCookie(‘hobby‘,‘篮球,足球‘);
$ck -> expire = time()+3600;
把$ck对象放入cookie组件里边
Yii::app()->request->cookies[‘hobby‘] = $ck;
$ck2 = new CHttpCookie(‘sex‘,‘nan‘);
$ck2 -> expire = time()+3600;
把$ck对象放入cookie组件里边
Yii::app()->request->cookies[‘sex‘] = $ck2;
echo "cookie make success";
}
function actionC2(){
访问cookie
echo Yii::app()->request->cookies[‘hobby‘],"
";
echo Yii::app()->request->cookies[‘sex‘];
}
function actionC3(){
删除cookie
unset(Yii::app()->request->cookies[‘sex‘]);
}
以上是关于yii2.0框架中session与cookie的用法的主要内容,如果未能解决你的问题,请参考以下文章