php手记之07-tp5 cookie与session

放肆的年华 提交于 2019-12-06 07:08:39

ThinkPHP采用

01-think\facade\Cookie类提供Cookie支持。

02-think\Cookie

配置文件位于 config/cookie.php中,一般会配置一个超时时间。

# 设置
// 设置Cookie 有效期为 3600秒
Cookie::set('name','value',3600);
cookie('name', 'value', 3600);
// 设置Cookie 有效期为 3600秒
Cookie::set('name','value',3600);
// 设置cookie 前缀为think_
Cookie::set('name','value',['prefix'=>'think_','expire'=>3600]);
// 支持数组
Cookie::set('name',[1,2,3]);
# 判断是否存在 Cookie::has('name'); cookie('?name') # 获取 Cookie::get('name'); cookie('name'); # 删除 Cookie::delete('name'); cookie('name', null);
// 清空指定前缀的cookieCookie::clear('think_');// 清除
cookie(null, 'think_');如果不指定前缀,不能做清空操作

session

配置文件位于 config/session.php中

# 设置
Session::set('name','thinkphp');
session('name', 'thinkphp');

# 闪存
Session::flash('name','value');

# 判断是否存在
Session::has('name');
session('?name');
# 取值
Session::get('name');
session('name');

# 删除
Session::delete('name');
session('name', null);
// 清除session(当前作用域)
Session::clear();
// 清除think作用域
Session::clear('think');
// 赋值(当前作用域)
session('name', 'thinkphp');
// 赋值think作用域
session('name', 'thinkphp', 'think');
// 指定当前作用域
Session::prefix('think');
// 取值并删除(如果name的值不存在,返回Null。)
Session::pull('name');
// 清除当前请求有效的session
Session::flush();
// 设置session 并且在下一次请求之前有效,再请求一次就无效了。
Session::flash('name','value');
闪存的使用:

 

 

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!