yii操作session实例简介,yiisession实例
本文简述了Yii框架中使用session的方法,具体步骤如下:
一、与标准php代码的区别:
在Yii框架中,你不需要像标准PHP代码那样使用session_start(),
在Yii框架中,autoStart 属性缺省被设置为true,所以,
虽然没有使用session_start(),你仍然可以使用$_SESSION全局变量,但最好使用Yii框架封装的Yii::app->session
二、session变量的使用:
设置session:
Yii::app()->session['var']='value';
使用session:
echo Yii::app()->session['var'];
移除session:
unset(Yii::app()->session['var']);
更为复杂一点的使用时如何配置你的session
配置项可设在 protected/config/main.php的components中:
'session'=>array( 'autoStart'=>false(/true), 'sessionName'=>'Site Access', 'cookieMode'=>'only', 'savePath'='/path/to/new/directory', ),
将session保持在数据库的设置:
'session' => array ( 'class' => 'system.web.CDbHttpSession', 'connectionID' => 'db', 'sessionTableName' => 'actual_table_name', ),
此外,为了调试,有时需要知道当前用户的session ID,
该值就在:
Yii::app()->session->sessionID
最后,当用户退出登录(logout),你需要消除痕迹,可使用:
Yii::app()->session->clear()
移去所有session变量,然后,调用
Yii::app()->session->destroy()
移去存储在服务器端的session数据。
你首先要在main.php中设置session timeout,'sessionTimeoutSeconds'=>300,然后在yii::app()->user->setState('userSessionTimeout',time()+Yii::app()->params['sessionTimeoutSeconds']);页面进行调用。
希望可以帮到你
session不是直接使用就可以了吗?

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Zend Studio 13.0.1
Powerful PHP integrated development environment

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver CS6
Visual web development tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.