逢琅快鹊硼袖坎暗猜龋护轰纳
打开惯例配置文件,复制session的配置
config.php:
1.设置session值
<?php return [ 'session' => [ 'id' => '', // SESSION_ID的提交变量,解决flash上传跨域 'var_session_id' => '', // SESSION 前缀 'prefix' => 'think', // 驱动方式 支持redis memcache memcached 'type' => '', // 是否自动开启 SESSION 'auto_start' => true, 'httponly' => true, 'secure' => false, ], ];
1.设置session的值
<?php namespace app\index\controller; use think\Controller; use think\Session; class Index extends Controller { public function index() { Session::set('name',"ThinkPHP5"); dump($_SESSION); } }
执行:
其中:think就是config中设置的文件名的前缀
即该数组是一个二维数组,think即作用域
2.赋值作用域
<?php namespace app\index\controller; use think\Controller; use think\Session; class Index extends Controller { public function index() { Session::set('name',"ThinkPHP5"); Session::set('name2','php','think2'); dump($_SESSION); } }
执行:
3.判断name是否赋值
<?php namespace app\index\controller; use think\Controller; use think\Session; class Index extends Controller { public function index() { //判断(当前作用域)是否赋值 echo Session::has('name'); } }
执行:
判断当前作用域(think)下name2是否赋值
<?php namespace app\index\controller; use think\Controller; use think\Session; class Index extends Controller { public function index() { echo Session::has('name2'); } }
执行,无显示表明在think作用域下没有赋值
4.判断think作用域下name是否赋值
<?php namespace app\index\controller; use think\Controller; use think\Session; class Index extends Controller { public function index() { echo Session::has('name','think'); } }
执行:
判断think2作用域下name2是否赋值
<?php namespace app\index\controller; use think\Controller; use think\Session; class Index extends Controller { public function index() { echo Session::has('name2','think2'); } }
执行:
5.取当前作用域的name值
<?php namespace app\index\controller; use think\Controller; use think\Session; class Index extends Controller { public function index() { echo Session::get('name'); } }
执行:
取值think2下的name2
<?php namespace app\index\controller; use think\Controller; use think\Session; class Index extends Controller { public function index() { echo Session::get('name2','think2'); } }
执行:
6.指定当前的作用域为think2并获取name2的值
<?php namespace app\index\controller; use think\Controller; use think\Session; class Index extends Controller { public function index() { Session::prefix('think2'); echo Session::get('name2'); } }
执行:
7.删除当前作用域下的name
<?php namespace app\index\controller; use think\Controller; use think\Session; class Index extends Controller { public function index() { Session::delete('name'); dump($_SESSION); } }
执行:
删除think2作用域下的name2
<?php namespace app\index\controller; use think\Controller; use think\Session; class Index extends Controller { public function index() { Session::delete('name2','think2'); dump($_SESSION); } }
执行:
8.清除session当前的作用域
<?php namespace app\index\controller; use think\Controller; use think\Session; class Index extends Controller { public function index() { Session::clear(); dump($_SESSION); } }
执行:
清除think2作用域
<?php namespace app\index\controller; use think\Controller; use think\Session; class Index extends Controller { public function index() { Session::clear('think2'); dump($_SESSION); } }
执行:
9.当前作用域赋值,赋二维数组
<?php namespace app\index\controller; use think\Controller; use think\Session; class Index extends Controller { public function index() { Session::set('name.item','thinkphp'); dump($_SESSION); } }
执行:
10.判断当前作用域中item.name是否赋值
<?php namespace app\index\controller; use think\Controller; use think\Session; class Index extends Controller { public function index() { echo Session::has('name.item'); } }
执行:
11.获取当前作用域中item.name的值
<?php namespace app\index\controller; use think\Controller; use think\Session; class Index extends Controller { public function index() { echo Session::get('name.item'); } }
执行:
12.删除当前作用域下的item.name值
<?php namespace app\index\controller; use think\Controller; use think\Session; class Index extends Controller { public function index() { Session::delete('name.item'); dump($_SESSION); } }
执行:
13.清空Session
<?php namespace app\index\controller; use think\Controller; use think\Session; class Index extends Controller { public function index() { $_SESSION=[]; dump($_SESSION); } }
执行:
助手函数的使用查看手册即可
session在模板中的使用
D:\phpStudy\PHPTutorial\WWW\tp5\application\index\controller\Index.php:
<?php namespace app\index\controller; use think\Controller; use think\Session; class Index extends Controller { public function index() { Session::set('name','mengmianren'); return $this->fetch(); } }
D:\phpStudy\PHPTutorial\WWW\tp5\application\index\view\index\index.html:
<p>用户名{$Request.session.name}</p>
执行:
瓮拔腔摆姬傅檬湃驮炯虎敬辑