🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
## 概述 ThinkPHP采用`think\facade\Cookie`类提供Cookie支持。 ## 配置 配置文件位于配置目录下的`cookie.php`文件,无需手动初始化,系统会自动在调用之前进行Cookie初始化工作。 ## 基本操作 ### 初始化 ~~~ // cookie初始化 Cookie::init(['prefix'=>'think_','expire'=>3600,'path'=>'/']); // 指定当前前缀 Cookie::prefix('think_'); ~~~ 支持的参数及默认值如下: ~~~ // cookie 名称前缀 'prefix' => '', // cookie 保存时间 'expire' => 0, // cookie 保存路径 'path' => '/', // cookie 有效域名 'domain' => '', // cookie 启用安全传输 'secure' => false, // httponly设置 'httponly' => '', // 是否使用 setcookie 'setcookie' => true, ~~~ ### 设置 ~~~ // 设置Cookie 有效期为 3600秒 Cookie::set('name','value',3600); // 设置cookie 前缀为think_ Cookie::set('name','value',['prefix'=>'think_','expire'=>3600]); // 支持数组 Cookie::set('name',[1,2,3]); ~~~ ### 永久保存 ~~~ // 永久保存Cookie Cookie::forever('name','value'); ~~~ ### 判断 ~~~ Cookie::has('name'); // 判断指定前缀的cookie值是否存在 Cookie::has('name','think_'); ~~~ ### 获取 ~~~ Cookie::get('name'); // 获取指定前缀的cookie值 Cookie::get('name','think_'); ~~~ ### 删除 ~~~ //删除cookie Cookie::delete('name'); // 删除指定前缀的cookie Cookie::delete('name','think_'); ~~~ ### 清空 ~~~ // 清空指定前缀的cookie Cookie::clear('think_'); ~~~ >[danger] 如果不指定前缀,不能做清空操作 ## 助手函数 系统提供了cookie助手函数用于基本的cookie操作,例如: ~~~ // 初始化 cookie(['prefix' => 'think_', 'expire' => 3600]); // 设置 cookie('name', 'value', 3600); // 获取 echo cookie('name'); // 删除 cookie('name', null); // 清除 cookie(null, 'think_'); ~~~