首页 > 代码库 > 用thinkphp操作session

用thinkphp操作session

写了一段代码,对session进行一些常用的操作:

<?phpnamespace Home\Controller;use Think\Controller;class Demo1Controller extends Controller {    public function index(){        $user = M("city")->select();                 $this->assign(‘list‘,$user);                 $this->display();    }        //session赋值    public function test1() {        session(‘username‘,‘admin‘);        session(‘useralias‘,‘管理员‘);                echo ‘session赋值‘;    }        //session取值    public function test2() {        echo ‘session取值<br />‘;        echo ‘username:‘.session(‘username‘).‘<br/>‘;        echo ‘useralias:‘.session(‘useralias‘).‘<br/>‘;    }        //删除session    public function test3() {        session(‘username‘,null);        session(‘useralias‘,null);            echo ‘删除session‘;    }        //判断session是否存在    public function test4() {        if(session(‘?username‘)) {            echo ‘username存在<br/>‘;        } else {            echo ‘username不存在<br/>‘;        }                if(session(‘?useralias‘)) {            echo ‘useralias存在‘;        } else {            echo ‘useralias不存在‘;        }    }}

 

用thinkphp操作session