# 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>);
```
```
- 脕茫隆垄脨貌脩脭
- 脕茫隆垄脨貌脩脭
- 脪禄隆垄禄霉麓隆
- 脪禄隆垄禄霉麓隆
- 露镁隆垄URL潞脥脗路脫脡
- 露镁隆垄URL潞脥脗路脫脡
- 脠媒隆垄脟毛脟贸潞脥脧矛脫娄
- 脠媒隆垄脟毛脟贸潞脥脧矛脫娄
- 脣脛隆垄脢媒戮脻驴芒
- 脣脛隆垄脢媒戮脻驴芒
- 脦氓隆垄虏茅脩炉脫茂脩脭
- 脦氓隆垄虏茅脩炉脫茂脩脭
- 脕霉隆垄脛拢脨脥潞脥鹿脴脕陋
- 拢篓1拢漏脛拢脨脥露篓脪氓
- 拢篓2拢漏禄霉麓隆虏脵脳梅
- 拢篓3拢漏露脕脠隆脝梅潞脥脨脼赂脛脝梅
- 拢篓4拢漏脌脿脨脥脳陋禄禄潞脥脳脭露炉脥锚鲁脡
- 拢篓5拢漏虏茅脩炉路露脦搂
- 拢篓6拢漏脢盲脠毛潞脥脩茅脰陇
- 拢篓7拢漏鹿脴脕陋
- 拢篓8拢漏脛拢脨脥脢盲鲁枚
- 脝脽隆垄脢脫脥录潞脥脛拢掳氓
- 脝脽隆垄脢脫脥录潞脥脛拢掳氓
- 掳脣隆垄碌梅脢脭潞脥脠脮脰戮
- 掳脣隆垄碌梅脢脭潞脥脠脮脰戮
- 戮脜隆垄API驴陋路垄
- 戮脜隆垄API驴陋路垄
- 脢庐隆垄脙眉脕卯脨脨鹿陇戮脽
- 脢庐隆垄脙眉脕卯脨脨鹿陇戮脽
- 脢庐脪禄隆垄脌漏脮鹿
- 脢庐脪禄隆垄脌漏脮鹿
- 脢庐露镁隆垄脭脫脧卯
- Cookie
- Session
- 碌楼脭陋虏芒脢脭
- 脥录脧帽麓娄脌铆
- 脦脛录镁脡脧麓芦
- 脩茅脰陇脗毛
- 赂陆脗录
- A隆垄鲁拢录没脦脢脤芒录炉
- B隆垄3.2潞脥5.0脟酶卤冒
- C隆垄脰煤脢脰潞炉脢媒
- 路卢脥芒脝陋拢潞脩搂脧掳ThinkPHP5碌脛脮媒脠路脳脣脢脝
- 路卢脥芒脝陋拢潞脩搂脧掳ThinkPHP5碌脛脮媒脠路脳脣脢脝