ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
# Cookie ThinkPHP使用`think\Cookie`类提供Cookie支持。 ## 初始化 大多数情况下,我们不需要进行`Cookie`的初始化,系统会在调用`Cooie`类方法的时候自动根据`cache`配置参数初始化,如果需要可以手动使用`init`方法进行初始化设置,例如: ``` <pre class="calibre18"> ``` <span class="hljs-comment">// cookie初始化</span> Cookie::init([<span class="hljs-string">'prefix'</span>=><span class="hljs-string">'think_'</span>, <span class="hljs-string">'expire'</span>=><span class="hljs-number">3600</span>, <span class="hljs-string">'path'</span>=><span class="hljs-string">'/'</span>]); <span class="hljs-comment">// 单独指定当前前缀</span> Cookie::prefix(<span class="hljs-string">'think_'</span>); ``` ``` 支持的参数及默认值如下: ``` <pre class="calibre18"> ``` <span class="hljs-comment">// cookie 名称前缀</span><span class="hljs-string">'prefix'</span> => <span class="hljs-string">''</span>, <span class="hljs-comment">// cookie 保存时间</span><span class="hljs-string">'expire'</span> => <span class="hljs-number">0</span>, <span class="hljs-comment">// cookie 保存路径</span><span class="hljs-string">'path'</span> => <span class="hljs-string">'/'</span>, <span class="hljs-comment">// cookie 有效域名</span><span class="hljs-string">'domain'</span> => <span class="hljs-string">''</span>, <span class="hljs-comment">// cookie 启用安全传输</span><span class="hljs-string">'secure'</span> => <span class="hljs-keyword">false</span>, <span class="hljs-comment">// httponly设置</span><span class="hljs-string">'httponly'</span> => <span class="hljs-string">''</span>, <span class="hljs-comment">// 是否使用 setcookie</span><span class="hljs-string">'setcookie'</span> => <span class="hljs-keyword">true</span>, ``` ``` ## 读取Cookie 建议的读取`Cookie`数据的方法是通过`Request`请求对象的`cookie`方法(原因和Session读取一样),例如: ``` <pre class="calibre18"> ``` <span class="hljs-keyword">namespace</span> <span class="hljs-title">app</span>\<span class="hljs-title">index</span>\<span class="hljs-title">controller</span>; <span class="hljs-keyword">use</span> <span class="hljs-title">think</span>\<span class="hljs-title">Request</span>; <span class="hljs-operator"><span class="hljs-keyword">class</span> <span class="hljs-title">User</span></span>{ <span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">index</span><span class="hljs-number">(Request <span class="hljs-regexp">$request</span>)</span></span>{ <span class="hljs-keyword">echo</span> <span class="hljs-regexp">$request</span>->cookie(<span class="hljs-string">'user_name'</span>); <span class="hljs-comment">// 读取二维数组</span><span class="hljs-keyword">echo</span> <span class="hljs-regexp">$request</span>->cookie(<span class="hljs-string">'user.name'</span>); } } ``` ``` > 通过Request对象读取Cookie数据支持默认值及过滤方法,因此也更加安全,并且支持多维数组的读取。 当然也支持使用`Cookie`类直接读取数据: ``` <pre class="calibre18"> ``` <span class="hljs-keyword">namespace</span> <span class="hljs-title">app</span>\<span class="hljs-title">index</span>\<span class="hljs-title">controller</span>; <span class="hljs-keyword">use</span> <span class="hljs-title">think</span>\<span class="hljs-title">Cookie</span>; <span class="hljs-operator"><span class="hljs-keyword">class</span> <span class="hljs-title">User</span></span>{ <span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">index</span><span class="hljs-number">()</span></span>{ <span class="hljs-keyword">echo</span> Cookie::get(<span class="hljs-string">'user_name'</span>); } } ``` ``` > Cookie::get方法不支持读取二维数组数据。 ## 模板输出 同样,可以使用下面的方法在模板文件中输出Cookie值。 ``` <pre class="calibre18"> ``` {<span class="hljs-regexp">$Request</span>.cookie.user_name} ``` ``` ## `Cookie`操作 下面是一些关于Cookie的基础操作方法。 ### 设置 ``` <pre class="calibre18"> ``` <span class="hljs-comment">// 设置Cookie 有效期为 3600秒</span> Cookie::set(<span class="hljs-string">'name'</span>,<span class="hljs-string">'value'</span>,<span class="hljs-number">3600</span>); <span class="hljs-comment">// 设置cookie 前缀为think_</span> Cookie::set(<span class="hljs-string">'name'</span>,<span class="hljs-string">'value'</span>,[<span class="hljs-string">'prefix'</span>=><span class="hljs-string">'think_'</span>, <span class="hljs-string">'expire'</span>=><span class="hljs-number">3600</span>]); <span class="hljs-comment">// 支持数组</span> Cookie::set(<span class="hljs-string">'name'</span>,[<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>]); ``` ``` ### 判断 ``` <pre class="calibre18"> ``` <span class="hljs-regexp">Cookie</span>::<span class="hljs-function">has</span>(<span class="hljs-string">'name'</span>); <span class="hljs-comment">// 判断指定前缀的cookie值是否存在</span><span class="hljs-regexp">Cookie</span>::<span class="hljs-function">has</span>(<span class="hljs-string">'name'</span>,<span class="hljs-string">'think_'</span>); ``` ``` ### 获取 ``` <pre class="calibre18"> ``` <span class="hljs-regexp">Cookie</span>::<span class="hljs-function">get</span>(<span class="hljs-string">'name'</span>); <span class="hljs-comment">// 获取指定前缀的cookie值</span><span class="hljs-regexp">Cookie</span>::<span class="hljs-function">get</span>(<span class="hljs-string">'name'</span>,<span class="hljs-string">'think_'</span>); ``` ``` ### 删除 //删除cookie ``` <pre class="calibre18"> ``` <span class="hljs-regexp">Cookie</span>::<span class="hljs-function">delete</span>(<span class="hljs-string">'name'</span>); <span class="hljs-comment">// 删除指定前缀的cookie</span><span class="hljs-regexp">Cookie</span>::<span class="hljs-function">delete</span>(<span class="hljs-string">'name'</span>,<span class="hljs-string">'think_'</span>); ``` ``` ### 清空 ``` <pre class="calibre18"> ``` <span class="hljs-comment">// 清空指定前缀的cookie</span><span class="hljs-regexp">Cookie</span>::<span class="hljs-function">clear</span>(<span class="hljs-string">'think_'</span>); ``` ``` > 注意,目前不支持清空所有的`Cookie`数据,如果必须这样做,请直接操作`$_COOKIE`变量。 ## 助手函数 系统提供了`cookie`助手函数用于基本的`Cookie`操作,可以完成前面的所有功能,例如: ``` <pre class="calibre42"> ``` <span class="hljs-comment">// 初始化</span> cookie([<span class="hljs-string">'prefix'</span> => <span class="hljs-string">'think_'</span>, <span class="hljs-string">'expire'</span> => <span class="hljs-number">3600</span>]); <span class="hljs-comment">// 设置</span> cookie(<span class="hljs-string">'name'</span>, <span class="hljs-string">'value'</span>, <span class="hljs-number">3600</span>); <span class="hljs-comment">// 判断</span> cookie(<span class="hljs-string">'?name'</span>); <span class="hljs-comment">// 获取</span><span class="hljs-keyword">echo</span> cookie(<span class="hljs-string">'name'</span>); <span class="hljs-comment">// 删除</span> cookie(<span class="hljs-string">'name'</span>, <span class="hljs-keyword">null</span>); <span class="hljs-comment">// 清除</span> cookie(<span class="hljs-keyword">null</span>, <span class="hljs-string">'think_'</span>); ``` ```