我们在开发项目中南面使用到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']);
}