企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
<article><h1>Laravel 的 HTTP 会话机制</h1><ul><li><a href="#introduction">简介</a><ul><li><a href="#configuration">配置</a></li><li><a href="#driver-prerequisites">驱动条件</a></li></ul></li><li><a href="#using-the-session">使用 Session</a><ul><li><a href="#retrieving-data">获取 Session 数据</a></li><li><a href="#storing-data">存储 Session 数据</a></li><li><a href="#flash-data">闪存数据到 Session</a></li><li><a href="#deleting-data">删除 Session 数据</a></li><li><a href="#regenerating-the-session-id">重新生成 Session ID</a></li></ul></li><li><a href="#adding-custom-session-drivers">添加自定义 Session 驱动</a><ul><li><a href="#implementing-the-driver">实现驱动</a></li><li><a href="#registering-the-driver">注册驱动</a></li></ul></li></ul><p><a name="introduction"></a></p><h2><a href="#introduction">简介</a></h2><p>由于 HTTP 是无状态的,Session 提供了一种在多个请求之间存储有关用户信息的方法。Laravel 附带支持了多种 Session 后端驱动,它们都可以通过语义化统一的 API 访问。Laravel 本身支持比较热门的 Session 后端驱动,如 <a href="https://memcached.org">Memcached</a>、<a href="http://redis.io">Redis</a> 和数据库。</p><p><a name="configuration"></a></p><h3>配置</h3><p>Session 相关的配置文件存储在 <code class=" language-php">config<span class="token operator">/</span>session<span class="token punctuation">.</span>php</code>。请务必查看此文件中对于你可用的选项。默认设置下,Laravel 的配置是使用文件作为 Session 驱动,大多数情况下能够运行良好。在生产环境下,你可以考虑使用 <code class=" language-php">memcached</code> 或 <code class=" language-php">redis</code> 驱动来达到更出色的性能表现。</p><p>Session 配置的 <code class=" language-php">driver</code> 的选项定义了每次请求的 Session 数据的存储位置。Laravel 附带了几个不错且可开箱即用的驱动:</p><div class="content-list"><ul><li><code class=" language-php">file</code> - 将 Session 保存在 <code class=" language-php">storage<span class="token operator">/</span>framework<span class="token operator">/</span>sessions</code>。</li><li><code class=" language-php">cookie</code> - Session 保存在安全加密的 Cookie 中。</li><li><code class=" language-php">database</code> - Session 保存在关系型数据库。</li><li><code class=" language-php">memcached</code> / <code class=" language-php">redis</code> - 将 Sessions 保存在其中一个快速且基于缓存的存储系统中。</li><li><code class=" language-php"><span class="token keyword">array</span></code> - 将 Sessions 保存在简单的 PHP 数组中,并只存在于本次请求.</li></ul></div><blockquote class="has-icon tip"><p><div class="flag"><span class="svg"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:a="http://ns.adobe.com/AdobeSVGViewerExtensions/3.0/" version="1.1" x="0px" y="0px" width="56.6px" height="87.5px" viewBox="0 0 56.6 87.5" enable-background="new 0 0 56.6 87.5" xml:space="preserve"><path fill="#FFFFFF" d="M28.7 64.5c-1.4 0-2.5-1.1-2.5-2.5v-5.7 -5V41c0-1.4 1.1-2.5 2.5-2.5s2.5 1.1 2.5 2.5v10.1 5 5.8C31.2 63.4 30.1 64.5 28.7 64.5zM26.4 0.1C11.9 1 0.3 13.1 0 27.7c-0.1 7.9 3 15.2 8.2 20.4 0.5 0.5 0.8 1 1 1.7l3.1 13.1c0.3 1.1 1.3 1.9 2.4 1.9 0.3 0 0.7-0.1 1.1-0.2 1.1-0.5 1.6-1.8 1.4-3l-2-8.4 -0.4-1.8c-0.7-2.9-2-5.7-4-8 -1-1.2-2-2.5-2.7-3.9C5.8 35.3 4.7 30.3 5.4 25 6.7 14.5 15.2 6.3 25.6 5.1c13.9-1.5 25.8 9.4 25.8 23 0 4.1-1.1 7.9-2.9 11.2 -0.8 1.4-1.7 2.7-2.7 3.9 -2 2.3-3.3 5-4 8L41.4 53l-2 8.4c-0.3 1.2 0.3 2.5 1.4 3 0.3 0.2 0.7 0.2 1.1 0.2 1.1 0 2.2-0.8 2.4-1.9l3.1-13.1c0.2-0.6 0.5-1.2 1-1.7 5-5.1 8.2-12.1 8.2-19.8C56.4 12 42.8-1 26.4 0.1zM43.7 69.6c0 0.5-0.1 0.9-0.3 1.3 -0.4 0.8-0.7 1.6-0.9 2.5 -0.7 3-2 8.6-2 8.6 -1.3 3.2-4.4 5.5-7.9 5.5h-4.1H28h-0.5 -3.6c-3.5 0-6.7-2.4-7.9-5.7l-0.1-0.4 -1.8-7.8c-0.4-1.1-0.8-2.1-1.2-3.1 -0.1-0.3-0.2-0.5-0.2-0.9 0.1-1.3 1.3-2.1 2.6-2.1H41C42.4 67.5 43.6 68.2 43.7 69.6zM37.7 72.5H26.9c-4.2 0-7.2 3.9-6.3 7.9 0.6 1.3 1.8 2.1 3.2 2.1h4.1 0.5 0.5 3.6c1.4 0 2.7-0.8 3.2-2.1L37.7 72.5z"></path></svg></span></div> 数组驱动一般用于 <a href="/docs/5.4/testing">测试</a> 防止存储在 Session 的数据被持久化。</p></blockquote><p><a name="driver-prerequisites"></a></p><h3>驱动条件</h3><h4>数据库</h4><p>使用 数据库 作为 Session 驱动时,你需要创建一张包含 Session 各项数据的表。以下例子是使用 <code class=" language-php">Schema</code> 建表:</p><pre class=" language-php"><code class=" language-php"><span class="token scope">Schema<span class="token punctuation">::</span></span><span class="token function">create<span class="token punctuation">(</span></span><span class="token string">'sessions'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$table</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token variable">$table</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">string<span class="token punctuation">(</span></span><span class="token string">'id'</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">unique<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token variable">$table</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">integer<span class="token punctuation">(</span></span><span class="token string">'user_id'</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">nullable<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token variable">$table</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">string<span class="token punctuation">(</span></span><span class="token string">'ip_address'</span><span class="token punctuation">,</span> <span class="token number">45</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">nullable<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token variable">$table</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">text<span class="token punctuation">(</span></span><span class="token string">'user_agent'</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">nullable<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token variable">$table</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">text<span class="token punctuation">(</span></span><span class="token string">'payload'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token variable">$table</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">integer<span class="token punctuation">(</span></span><span class="token string">'last_activity'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p>也可以使用 <code class=" language-php">Artisan</code> 的 <code class=" language-php">session<span class="token punctuation">:</span>table</code> 命令生成一个迁移文件:</p><pre class=" language-php"><code class=" language-php">php artisan session<span class="token punctuation">:</span>table php artisan migrate</code></pre><h4>Redis</h4><p>在使用 Redis 作为 Session 驱动之前,你需要通过 Composer 安装 <code class=" language-php">predis<span class="token operator">/</span>predis</code> 扩展包(~1.0)。你还需要在 <code class=" language-php">database</code> 配置文件中指定 Redis 连接参数信息。在 Session 配置文件中的 <code class=" language-php">connection</code> 选项中指定 Session 使用的 Redis 连接。</p><p><a name="using-the-session"></a></p><h2><a href="#using-the-session">使用 Session</a></h2><p><a name="retrieving-data"></a></p><h3>获取 Session 数据</h3><p>Laravel 中有两种主要的方式使用 Session 数据的方式:一种是全局的辅助函数 <code class=" language-php">session</code>,另一种是通过 HTTP 请求实例。首先,我们先看一下第二种方法,就是通过具有控制器方法类型提示的 HTTP 请求实例来访问 Session。请记住,控制器方法的依赖关系会通过 Laravel 的 <a href="/docs/5.4/container">服务容器</a>自动注入:</p><pre class=" language-php"><code class=" language-php"><span class="token delimiter">&lt;?php</span> <span class="token keyword">namespace</span> <span class="token package">App<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Controllers</span><span class="token punctuation">;</span> <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Request</span><span class="token punctuation">;</span> <span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Controllers<span class="token punctuation">\</span>Controller</span><span class="token punctuation">;</span> <span class="token keyword">class</span> <span class="token class-name">UserController</span> <span class="token keyword">extends</span> <span class="token class-name">Controller</span> <span class="token punctuation">{</span> <span class="token comment" spellcheck="true">/** * 展示用户个人信息 * * @param Request $request * @param int $id * @return Response */</span> <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">show<span class="token punctuation">(</span></span>Request <span class="token variable">$request</span><span class="token punctuation">,</span> <span class="token variable">$id</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token variable">$value</span> <span class="token operator">=</span> <span class="token variable">$request</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">session<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">get<span class="token punctuation">(</span></span><span class="token string">'key'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true"> // </span> <span class="token punctuation">}</span> <span class="token punctuation">}</span></code></pre><p>当从 Session 获取值时,你也可以传递一个默认值作为 <code class=" language-php">get</code> 方法的第二个参数。如果 Session 中并不存在指定的键值便会返回传入的默认值。若传递一个闭包作为 <code class=" language-php">get</code> 方法的默认值且请求的键值并不存在时,此时 <code class=" language-php">get</code> 方法会返回这个闭包函数运行后的返回值:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$value</span> <span class="token operator">=</span> <span class="token variable">$request</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">session<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">get<span class="token punctuation">(</span></span><span class="token string">'key'</span><span class="token punctuation">,</span> <span class="token string">'default'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token variable">$value</span> <span class="token operator">=</span> <span class="token variable">$request</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">session<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">get<span class="token punctuation">(</span></span><span class="token string">'key'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> <span class="token string">'default'</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><h4>全局 Session 辅助函数</h4><p>你也可以使用全局的 PHP 函数 <code class=" language-php">session</code> 来获取和存储 Session 数据。 使用单个字符串类型的值作为参数调用 <code class=" language-php">session</code> 函数时,它将返回字该符串参数对应的 Session 键值。当使用一个 key / value 键值对数组作为参数调用 <code class=" language-php">session</code> 函数时,传入的键值将会存入 Session:</p><pre class=" language-php"><code class=" language-php"><span class="token scope">Route<span class="token punctuation">::</span></span><span class="token function">get<span class="token punctuation">(</span></span><span class="token string">'home'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment" spellcheck="true"> // 获取 Session 中的一条数据... </span> <span class="token variable">$value</span> <span class="token operator">=</span> <span class="token function">session<span class="token punctuation">(</span></span><span class="token string">'key'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true"> // 指定一个默认值... </span> <span class="token variable">$value</span> <span class="token operator">=</span> <span class="token function">session<span class="token punctuation">(</span></span><span class="token string">'key'</span><span class="token punctuation">,</span> <span class="token string">'default'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true"> // 存储一条数据至 Session 中... </span> <span class="token function">session<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token string">'key'</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token string">'value'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><blockquote class="has-icon tip"><p><div class="flag"><span class="svg"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:a="http://ns.adobe.com/AdobeSVGViewerExtensions/3.0/" version="1.1" x="0px" y="0px" width="56.6px" height="87.5px" viewBox="0 0 56.6 87.5" enable-background="new 0 0 56.6 87.5" xml:space="preserve"><path fill="#FFFFFF" d="M28.7 64.5c-1.4 0-2.5-1.1-2.5-2.5v-5.7 -5V41c0-1.4 1.1-2.5 2.5-2.5s2.5 1.1 2.5 2.5v10.1 5 5.8C31.2 63.4 30.1 64.5 28.7 64.5zM26.4 0.1C11.9 1 0.3 13.1 0 27.7c-0.1 7.9 3 15.2 8.2 20.4 0.5 0.5 0.8 1 1 1.7l3.1 13.1c0.3 1.1 1.3 1.9 2.4 1.9 0.3 0 0.7-0.1 1.1-0.2 1.1-0.5 1.6-1.8 1.4-3l-2-8.4 -0.4-1.8c-0.7-2.9-2-5.7-4-8 -1-1.2-2-2.5-2.7-3.9C5.8 35.3 4.7 30.3 5.4 25 6.7 14.5 15.2 6.3 25.6 5.1c13.9-1.5 25.8 9.4 25.8 23 0 4.1-1.1 7.9-2.9 11.2 -0.8 1.4-1.7 2.7-2.7 3.9 -2 2.3-3.3 5-4 8L41.4 53l-2 8.4c-0.3 1.2 0.3 2.5 1.4 3 0.3 0.2 0.7 0.2 1.1 0.2 1.1 0 2.2-0.8 2.4-1.9l3.1-13.1c0.2-0.6 0.5-1.2 1-1.7 5-5.1 8.2-12.1 8.2-19.8C56.4 12 42.8-1 26.4 0.1zM43.7 69.6c0 0.5-0.1 0.9-0.3 1.3 -0.4 0.8-0.7 1.6-0.9 2.5 -0.7 3-2 8.6-2 8.6 -1.3 3.2-4.4 5.5-7.9 5.5h-4.1H28h-0.5 -3.6c-3.5 0-6.7-2.4-7.9-5.7l-0.1-0.4 -1.8-7.8c-0.4-1.1-0.8-2.1-1.2-3.1 -0.1-0.3-0.2-0.5-0.2-0.9 0.1-1.3 1.3-2.1 2.6-2.1H41C42.4 67.5 43.6 68.2 43.7 69.6zM37.7 72.5H26.9c-4.2 0-7.2 3.9-6.3 7.9 0.6 1.3 1.8 2.1 3.2 2.1h4.1 0.5 0.5 3.6c1.4 0 2.7-0.8 3.2-2.1L37.7 72.5z"></path></svg></span></div> HTTP 请求实例与 <code class=" language-php">Session</code> 全局辅助函数使用 Session 并没有实质上的区别。两种方法都是可以通过 <code class=" language-php">assertSessionHas</code> 方法 <a href="/docs/5.4/testing">测试</a> ,<code class=" language-php">assertSessionHas</code> 方法在所有的测试用例都是可用的。关于测试的更多信息,请阅读文档 <a href="/docs/5.4/testing">测试</a></p></blockquote><h4>获取所有 Session 数据</h4><p>如果你想要获取所有的 Session 数据,可以使用 <code class=" language-php">all</code> 方法:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$data</span> <span class="token operator">=</span> <span class="token variable">$request</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">session<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">all<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><h4>判断某个 Session 值是否存在</h4><p>使用 <code class=" language-php">has</code> 方法检查某个值是否存在于 Session 内,如果该值存在并且不为 null,那么则返回 true:</p><pre class=" language-php"><code class=" language-php"><span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token variable">$request</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">session<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">has<span class="token punctuation">(</span></span><span class="token string">'users'</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment" spellcheck="true"> // </span><span class="token punctuation">}</span></code></pre><p>在判断值是否在 Session 中是否存时,如果该值可能为 null,你需要使用 exists 方法,如果该值存在,那么则返回 true:</p><pre class=" language-php"><code class=" language-php"><span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token variable">$request</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">session<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">exists<span class="token punctuation">(</span></span><span class="token string">'users'</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment" spellcheck="true"> // </span><span class="token punctuation">}</span></code></pre><p><a name="storing-data"></a></p><h3>存储 Session 数据</h3><p>存储数据到 Session,你可用使用 <code class=" language-php">put</code> 方法,或者 <code class=" language-php">session</code> 辅助函数。</p><pre class=" language-php"><code class=" language-php"><span class="token comment" spellcheck="true">// 通过 HTTP 请求实例... </span><span class="token variable">$request</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">session<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">put<span class="token punctuation">(</span></span><span class="token string">'key'</span><span class="token punctuation">,</span> <span class="token string">'value'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true"> // 通过全局辅助函数 </span><span class="token function">session<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token string">'key'</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token string">'value'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><h4>保存数据进 Session 数组值中</h4><p>push 方法可以将一个新的值加入至一个 Session 数组内。例如,假设 user.teams 这个键是包含团队名称的数组,你可以将一个新的值加入此数组中。比如这样:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$request</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">session<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">push<span class="token punctuation">(</span></span><span class="token string">'user.teams'</span><span class="token punctuation">,</span> <span class="token string">'developers'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><h4>从 Session 中取出并删除数据</h4><p><code class=" language-php">pull</code> 方法将把数据从 Session 内取出,并且删除:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$value</span> <span class="token operator">=</span> <span class="token variable">$request</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">session<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">pull<span class="token punctuation">(</span></span><span class="token string">'key'</span><span class="token punctuation">,</span> <span class="token string">'default'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p><a name="flash-data"></a></p><h3>闪存数据到 Session</h3><p>有时候你想存入一条缓存的数据,让它只在下一次的请求内有效,则可以使用 <code class=" language-php">flash</code> 方法。使用这个方法保存 session,只能将数据保留到下个 HTTP 请求,然后就会被自动删除。闪存数据在短期的状态消息中很有用:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$request</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">session<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">flash<span class="token punctuation">(</span></span><span class="token string">'status'</span><span class="token punctuation">,</span> <span class="token string">'Task was successful!'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p>如果需要保留闪存数据给更多请求,可以使用 <code class=" language-php">reflash</code> 方法,这将会将所有的闪存数据保留给额外的请求。如果想保留特定的闪存数据,则可以使用 <code class=" language-php">keep</code> 方法:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$request</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">session<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">reflash<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token variable">$request</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">session<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">keep<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token string">'username'</span><span class="token punctuation">,</span> <span class="token string">'email'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p><a name="deleting-data"></a></p><h3>删除 Session 数据</h3><p><code class=" language-php">forget</code> 方法可以从 Session 内删除一条数据。如果你想删除 Session 内所有数据,则可以使用 <code class=" language-php">flush</code> 方法:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$request</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">session<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">forget<span class="token punctuation">(</span></span><span class="token string">'key'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token variable">$request</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">session<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">flush<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p><a name="regenerating-the-session-id"></a></p><h3>重新生成 Session ID</h3><p>重新生成 Session ID,通常时为了防止恶意用户利用 <a href="https://en.wikipedia.org/wiki/Session_fixation">session fixation</a> 对应用进行攻击。</p><p>如果你使用了内置函数 <code class=" language-php">LoginController</code>,那么 Laravel 会自动重新生成 Session ID,否则,你需要手动使用 <code class=" language-php">regenerate</code> 方法重新生成 Session ID</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$request</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">session<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">regenerate<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p><a name="adding-custom-session-drivers"></a></p><h2><a href="#adding-custom-session-drivers">添加自定义 Session 驱动</a></h2><p><a name="implementing-the-driver"></a></p><h4>实现驱动</h4><p>你自定义的 Session 驱动必须实现 <code class=" language-php">SessionHandlerInterface</code> 接口。这个接口包含了一些基本需要实现的方法。一个基本的 MongoDB 实现应该看起来像这样:</p><pre class=" language-php"><code class=" language-php"><span class="token delimiter">&lt;?php</span> <span class="token keyword">namespace</span> <span class="token package">App<span class="token punctuation">\</span>Extensions</span><span class="token punctuation">;</span> <span class="token keyword">class</span> <span class="token class-name">MongoHandler</span> <span class="token keyword">implements</span> <span class="token class-name">SessionHandlerInterface</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">open<span class="token punctuation">(</span></span><span class="token variable">$savePath</span><span class="token punctuation">,</span> <span class="token variable">$sessionName</span><span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token punctuation">}</span> <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">close<span class="token punctuation">(</span></span><span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token punctuation">}</span> <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">read<span class="token punctuation">(</span></span><span class="token variable">$sessionId</span><span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token punctuation">}</span> <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">write<span class="token punctuation">(</span></span><span class="token variable">$sessionId</span><span class="token punctuation">,</span> <span class="token variable">$data</span><span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token punctuation">}</span> <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">destroy<span class="token punctuation">(</span></span><span class="token variable">$sessionId</span><span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token punctuation">}</span> <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">gc<span class="token punctuation">(</span></span><span class="token variable">$lifetime</span><span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token punctuation">}</span> <span class="token punctuation">}</span></code></pre><blockquote class="has-icon tip"><p><div class="flag"><span class="svg"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:a="http://ns.adobe.com/AdobeSVGViewerExtensions/3.0/" version="1.1" x="0px" y="0px" width="56.6px" height="87.5px" viewBox="0 0 56.6 87.5" enable-background="new 0 0 56.6 87.5" xml:space="preserve"><path fill="#FFFFFF" d="M28.7 64.5c-1.4 0-2.5-1.1-2.5-2.5v-5.7 -5V41c0-1.4 1.1-2.5 2.5-2.5s2.5 1.1 2.5 2.5v10.1 5 5.8C31.2 63.4 30.1 64.5 28.7 64.5zM26.4 0.1C11.9 1 0.3 13.1 0 27.7c-0.1 7.9 3 15.2 8.2 20.4 0.5 0.5 0.8 1 1 1.7l3.1 13.1c0.3 1.1 1.3 1.9 2.4 1.9 0.3 0 0.7-0.1 1.1-0.2 1.1-0.5 1.6-1.8 1.4-3l-2-8.4 -0.4-1.8c-0.7-2.9-2-5.7-4-8 -1-1.2-2-2.5-2.7-3.9C5.8 35.3 4.7 30.3 5.4 25 6.7 14.5 15.2 6.3 25.6 5.1c13.9-1.5 25.8 9.4 25.8 23 0 4.1-1.1 7.9-2.9 11.2 -0.8 1.4-1.7 2.7-2.7 3.9 -2 2.3-3.3 5-4 8L41.4 53l-2 8.4c-0.3 1.2 0.3 2.5 1.4 3 0.3 0.2 0.7 0.2 1.1 0.2 1.1 0 2.2-0.8 2.4-1.9l3.1-13.1c0.2-0.6 0.5-1.2 1-1.7 5-5.1 8.2-12.1 8.2-19.8C56.4 12 42.8-1 26.4 0.1zM43.7 69.6c0 0.5-0.1 0.9-0.3 1.3 -0.4 0.8-0.7 1.6-0.9 2.5 -0.7 3-2 8.6-2 8.6 -1.3 3.2-4.4 5.5-7.9 5.5h-4.1H28h-0.5 -3.6c-3.5 0-6.7-2.4-7.9-5.7l-0.1-0.4 -1.8-7.8c-0.4-1.1-0.8-2.1-1.2-3.1 -0.1-0.3-0.2-0.5-0.2-0.9 0.1-1.3 1.3-2.1 2.6-2.1H41C42.4 67.5 43.6 68.2 43.7 69.6zM37.7 72.5H26.9c-4.2 0-7.2 3.9-6.3 7.9 0.6 1.3 1.8 2.1 3.2 2.1h4.1 0.5 0.5 3.6c1.4 0 2.7-0.8 3.2-2.1L37.7 72.5z"></path></svg></span></div> Laravel 默认没有附带扩展目录,你可以把它放在你喜欢的目录内。在下面这个例子中,我们创建了一个 <code class=" language-php">Extensions</code> 目录放置自定义的 <code class=" language-php">MongoHandler</code> 扩展。</p></blockquote><p>接口中的这些方法不太容易容易理解。让我们来快速了解每个方法的作用:</p><div class="content-list"><ul><li><code class=" language-php">open</code> 方法通常用于基于文件的 Session 存储系统。因为 Larvel 已经附带了一个 <code class=" language-php">file</code> 的驱动,所以在该方法中不需要放置任何代码。PHP 要求必需要有这个方法的实现,但你可以把这方法置空也没关系。</li><li><code class=" language-php">close</code> 方法跟 <code class=" language-php">open</code> 方法很相似,通常也可以被忽略。对大多数的驱动而言,此方法并不是需要的。</li><li><code class=" language-php">read</code> 方法应当返回与给定的 <code class=" language-php"><span class="token variable">$sessionId</span></code> 相匹配的 Session 数据的字符串版本。从这个自定义的驱动中获取或存储 Session 数据不需要做任何序列化或其它编码,因为 Laravel 已经为我们做了序列化。</li><li><code class=" language-php">write</code> 将与 <code class=" language-php"><span class="token variable">$sessionId</span></code> 关联的特定 <code class=" language-php"><span class="token variable">$data</span></code> 字符串,写入到持久化存储系统,如 MongoDB、Dynamo 等等。再次重申,你不需要做任何序列化或其它编码,因为 Laravel 会自动处理这些事情。</li><li><code class=" language-php">destroy</code> 方法从持久化存储中移除 <code class=" language-php"><span class="token variable">$sessionId</span></code> 对应的数据。</li><li><code class=" language-php">gc</code> 方法能销毁 <code class=" language-php"><span class="token variable">$lifetime</span></code> 之前的所有数据,<code class=" language-php"><span class="token variable">$lifetime</span></code> 是一个 UNIX 的时间戳。对本身拥有过期机制的系统如 Memcached 和 Redis 而言,该方法可以留空。</li></ul></div><p><a name="registering-the-driver"></a></p><h4>注册驱动</h4><p>在 Session 驱动实现了 <code class=" language-php">SessionHandlerInterface</code> 接口后,你还需要在框架中注册该驱动,将该扩展驱动添加到 Laravel Session 后端。你可以使用 <code class=" language-php">Session</code> Facade 的 <code class=" language-php">extend</code> 方法。在 <a href="/docs/5.4/providers">服务提供者</a> 的 <code class=" language-php">boot</code> 方法内调用 <code class=" language-php">extend</code> 方法。你可用使用已经存在的 <code class=" language-php">AppServiceProvider</code> 或者创建一个新的提供者。</p><pre class=" language-php"><code class=" language-php"><span class="token delimiter">&lt;?php</span> <span class="token keyword">namespace</span> <span class="token package">App<span class="token punctuation">\</span>Providers</span><span class="token punctuation">;</span> <span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Extensions<span class="token punctuation">\</span>MongoSessionStore</span><span class="token punctuation">;</span> <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Session</span><span class="token punctuation">;</span> <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>ServiceProvider</span><span class="token punctuation">;</span> <span class="token keyword">class</span> <span class="token class-name">SessionServiceProvider</span> <span class="token keyword">extends</span> <span class="token class-name">ServiceProvider</span> <span class="token punctuation">{</span> <span class="token comment" spellcheck="true">/** * 提供注册后运行的服务。 * * @return void */</span> <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">boot<span class="token punctuation">(</span></span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token scope">Session<span class="token punctuation">::</span></span><span class="token function">extend<span class="token punctuation">(</span></span><span class="token string">'mongo'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$app</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment" spellcheck="true"> // Return implementation of SessionHandlerInterface... </span> <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">MongoSessionStore</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment" spellcheck="true">/** * 在容器中注册绑定。 * * @return void */</span> <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">register<span class="token punctuation">(</span></span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment" spellcheck="true"> // </span> <span class="token punctuation">}</span> <span class="token punctuation">}</span></code></pre><p>一旦 Session 驱动被注册,则必须在 <code class=" language-php">config<span class="token operator">/</span>session<span class="token punctuation">.</span>php</code> 的配置文件内使用 <code class=" language-php">Mongo</code> 驱动。</p><h2>译者署名</h2><table><thead><tr><th>用户名</th><th>头像</th><th>职能</th><th>签名</th></tr></thead><tbody><tr><td><a href="http://weibo.com/wangkaibo">@王凯波</a></td><td><img class="avatar-66 rm-style" src="https://dn-phphub.qbox.me/uploads/avatars/1924_1487053084.jpeg?imageView2/1/w/100/h/100"></td><td>翻译</td><td>面向工资编程 😆 <a href="https://github.com/wangkaibo/">@wangkaibo</a></td></tr></tbody></table></article>