合规国际互联网加速 OSASE为企业客户提供高速稳定SD-WAN国际加速解决方案。 广告
# 会话 `ThinkPHP5.0`使用`think\Session`类进行`Session`的操作管理。 - - [Session初始化](http://ihavenolimitations.xyz/thinkphp/thinkphp5_quickstart/183828#-code-session-code-) - [读取Session](http://ihavenolimitations.xyz/thinkphp/thinkphp5_quickstart/183828#-code-session-code--1) - [SESSION操作](http://ihavenolimitations.xyz/thinkphp/thinkphp5_quickstart/183828#-code-session-code--2) - [助手函数](http://ihavenolimitations.xyz/thinkphp/thinkphp5_quickstart/183828#u52A9u624Bu51FDu6570) - [模板输出](http://ihavenolimitations.xyz/thinkphp/thinkphp5_quickstart/183828#u6A21u677Fu8F93u51FA) - [Session驱动](http://ihavenolimitations.xyz/thinkphp/thinkphp5_quickstart/183828#-code-session-code--3) - [Session示例](http://ihavenolimitations.xyz/thinkphp/thinkphp5_quickstart/183828#session-) ## `Session`初始化 大多数情况,我们不需要手动进行Session初始化操作。`ThinkPHP5`会在第一次调用`Session`类的时候按照配置的参数自动初始化和开启`Session`(如果`auto_start`设置为`true`的话),例如,我们在应用配置中添加如下配置: ``` <pre class="calibre18"> ``` <span class="hljs-string">'session'</span> => [ <span class="hljs-string">'prefix'</span> => <span class="hljs-string">'think'</span>, <span class="hljs-string">'type'</span> => <span class="hljs-string">''</span>, <span class="hljs-string">'auto_start'</span> => <span class="hljs-keyword">true</span>, ], ``` ``` 无需任何操作就可以直接调用`Session`类的相关方法,例如: ``` <pre class="calibre18"> ``` <span class="hljs-operator"><span class="hljs-title1">Session</span>:<span class="hljs-string">:<span class="hljs-function">set</span>(<span class="hljs-operator">'name'</span>,<span class="hljs-operator">'thinkphp'</span>)</span></span>; <span class="hljs-operator"><span class="hljs-title1">Session</span>:<span class="hljs-string">:<span class="hljs-function">get</span>(<span class="hljs-operator">'name'</span>)</span></span>; ``` ``` 如果你的应用不同模块需要不同的`session`配置参数,那么可以在模块配置文件中重新设置: ``` <pre class="calibre18"> ``` <span class="hljs-string">'session'</span> => [ <span class="hljs-string">'prefix'</span> => <span class="hljs-string">'module'</span>, <span class="hljs-string">'type'</span> => <span class="hljs-string">''</span>, <span class="hljs-string">'auto_start'</span> => <span class="hljs-keyword">true</span>, ], ``` ``` 或者在模块的公共文件中调用`init`方法进行初始化: ``` <pre class="calibre18"> ``` Session::init([ <span class="hljs-string">'prefix'</span> => <span class="hljs-string">'module'</span>, <span class="hljs-string">'type'</span> => <span class="hljs-string">''</span>, <span class="hljs-string">'auto_start'</span> => <span class="hljs-keyword">true</span>, ]); ``` ``` > 如果你没有使用Session类进行Session操作的话,例如直接操作`$_SESSION`,必须使用上面的方式手动初始化或者直接调用`session_start()`方法进行`session`初始化,但不建议直接操作`$_SESSION`全局变量。 ## 读取`Session` 建议的读取`Session`数据的方法是通过`Request`请求对象的`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>->session(<span class="hljs-string">'user_name'</span>); <span class="hljs-comment">// 读取二维数组</span><span class="hljs-keyword">echo</span> <span class="hljs-regexp">$request</span>->session(<span class="hljs-string">'user.name'</span>); } } ``` ``` > 通过Request对象读取Session数据支持默认值及过滤方法,因此也更加安全,并且支持多维数组的读取。 当然也支持使用`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">Session</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> Session::get(<span class="hljs-string">'user_name'</span>); <span class="hljs-keyword">echo</span> Session::get(<span class="hljs-string">'user.name'</span>); } } ``` ``` > `Session`类的`get`方法只支持最大二维数组的读取,而`Request`对象的`session`方法可以支持任意级别的二维数组获取。 ## `SESSION`操作 使用`think\Session`类进行`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">Session</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-comment">// 赋值(当前作用域)</span> Session::set(<span class="hljs-string">'name'</span>,<span class="hljs-string">'thinkphp'</span>); <span class="hljs-comment">// 赋值think作用域</span> Session::set(<span class="hljs-string">'name'</span>,<span class="hljs-string">'thinkphp'</span>,<span class="hljs-string">'think'</span>); <span class="hljs-comment">// 判断(当前作用域)是否赋值</span> Session::has(<span class="hljs-string">'name'</span>); <span class="hljs-comment">// 判断think作用域下面是否赋值</span> Session::has(<span class="hljs-string">'name'</span>,<span class="hljs-string">'think'</span>); <span class="hljs-comment">// 取值(当前作用域)</span> Session::get(<span class="hljs-string">'name'</span>); <span class="hljs-comment">// 取值think作用域</span> Session::get(<span class="hljs-string">'name'</span>,<span class="hljs-string">'think'</span>); <span class="hljs-comment">// 指定当前作用域</span> Session::prefix(<span class="hljs-string">'think'</span>); <span class="hljs-comment">// 删除(当前作用域)</span> Session::delete(<span class="hljs-string">'name'</span>); <span class="hljs-comment">// 删除think作用域下面的值</span> Session::delete(<span class="hljs-string">'name'</span>,<span class="hljs-string">'think'</span>); <span class="hljs-comment">// 清除session(当前作用域)</span> Session::clear(); <span class="hljs-comment">// 清除think作用域</span> Session::clear(<span class="hljs-string">'think'</span>); <span class="hljs-comment">// 赋值(当前作用域)</span> Session::set(<span class="hljs-string">'name.item'</span>,<span class="hljs-string">'thinkphp'</span>); <span class="hljs-comment">// 判断(当前作用域)是否赋值</span> Session::has(<span class="hljs-string">'name.item'</span>); <span class="hljs-comment">// 取值(当前作用域)</span> Session::get(<span class="hljs-string">'name.item'</span>); <span class="hljs-comment">// 删除(当前作用域)</span> Session::delete(<span class="hljs-string">'name.item'</span>); } } ``` ``` ## 助手函数 系统也提供了助手函数`session`完成相同的功能,例如: ``` <pre class="calibre18"> ``` <span class="hljs-comment">// 初始化session</span> session([ <span class="hljs-string">'prefix'</span> => <span class="hljs-string">'module'</span>, <span class="hljs-string">'type'</span> => <span class="hljs-string">''</span>, <span class="hljs-string">'auto_start'</span> => <span class="hljs-keyword">true</span>, ]); <span class="hljs-comment">// 赋值(当前作用域)</span> session(<span class="hljs-string">'name'</span>, <span class="hljs-string">'thinkphp'</span>); <span class="hljs-comment">// 赋值think作用域</span> session(<span class="hljs-string">'name'</span>, <span class="hljs-string">'thinkphp'</span>, <span class="hljs-string">'think'</span>); <span class="hljs-comment">// 判断(当前作用域)是否赋值</span> session(<span class="hljs-string">'?name'</span>); <span class="hljs-comment">// 取值(当前作用域)</span> session(<span class="hljs-string">'name'</span>); <span class="hljs-comment">// 取值think作用域</span> session(<span class="hljs-string">'name'</span>, <span class="hljs-string">''</span>, <span class="hljs-string">'think'</span>); <span class="hljs-comment">// 删除(当前作用域)</span> session(<span class="hljs-string">'name'</span>, <span class="hljs-keyword">null</span>); <span class="hljs-comment">// 清除session(当前作用域)</span> session(<span class="hljs-keyword">null</span>); <span class="hljs-comment">// 清除think作用域</span> session(<span class="hljs-keyword">null</span>, <span class="hljs-string">'think'</span>); ``` ``` ## 模板输出 如果需要在模板中输出Session数据,可以使用下面的方法: ``` <pre class="calibre18"> ``` {<span class="hljs-regexp">$Request</span>.session.user_name} ``` ``` 也可以支持二维数组的输出 ``` <pre class="calibre18"> ``` {<span class="hljs-regexp">$Request</span>.session.user.name} ``` ``` ## `Session`驱动 支持指定 Session 驱动,配置文件如下: ``` <pre class="calibre18"> ``` <span class="hljs-string">'session'</span> => [ <span class="hljs-string">'prefix'</span> => <span class="hljs-string">'module'</span>, <span class="hljs-string">'type'</span> => <span class="hljs-string">'redis'</span>, <span class="hljs-string">'auto_start'</span> => <span class="hljs-keyword">true</span>, <span class="hljs-comment">// redis主机</span><span class="hljs-string">'host'</span> => <span class="hljs-string">'127.0.0.1'</span>, <span class="hljs-comment">// redis端口</span><span class="hljs-string">'port'</span> => <span class="hljs-number">6379</span>, <span class="hljs-comment">// 密码</span><span class="hljs-string">'password'</span> => <span class="hljs-string">''</span>, ] ``` ``` 表示使用`redis`作为`session`类型。 ## 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">Controller</span>; <span class="hljs-keyword">use</span> <span class="hljs-title">think</span>\<span class="hljs-title">Session</span>; <span class="hljs-operator"><span class="hljs-keyword">class</span> <span class="hljs-title">Index</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Controller</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">return</span> <span class="hljs-regexp">$this</span>->fetch(); } <span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">save</span><span class="hljs-number">(<span class="hljs-regexp">$name</span>=<span class="hljs-string">''</span>)</span></span>{ Session::set(<span class="hljs-string">'user_name'</span>,<span class="hljs-regexp">$name</span>); <span class="hljs-regexp">$this</span>->success(<span class="hljs-string">'Session设置成功'</span>); } } ``` ``` 定义默认文件(`application/index/view/index.html`)如下: ``` <pre class="calibre18"> ``` <span class="hljs-regexp"><!doctype html></span><span class="hljs-regexp"><<span class="hljs-operator">html</span>></span><span class="hljs-regexp"><<span class="hljs-operator">head</span>></span><span class="hljs-regexp"><<span class="hljs-operator">meta</span> <span class="hljs-operator">charset</span>=<span class="hljs-string">"UTF-8"</span>></span><span class="hljs-regexp"><<span class="hljs-operator">title</span>></span>Session示例<span class="hljs-regexp"></<span class="hljs-operator">title</span>></span><span class="hljs-regexp"><<span class="hljs-operator">style</span>></span><span class="css"><span class="hljs-regexp">body</span> <span class="hljs-operator">{ <span class="hljs-operator"><span class="hljs-title1">font-family</span>:<span class="hljs-string"><span class="hljs-operator">"Microsoft Yahei"</span>,<span class="hljs-operator">"Helvetica Neue"</span>,Helvetica,Arial,sans-serif</span></span>; <span class="hljs-operator"><span class="hljs-title1">font-size</span>:<span class="hljs-string"><span class="hljs-number">16px</span></span></span>; <span class="hljs-operator"><span class="hljs-title1">padding</span>:<span class="hljs-string"><span class="hljs-number">5px</span></span></span>; }</span><span class="hljs-regexp">.form</span><span class="hljs-operator">{ <span class="hljs-operator"><span class="hljs-title1">padding</span>:<span class="hljs-string"> <span class="hljs-number">15px</span></span></span>; <span class="hljs-operator"><span class="hljs-title1">font-size</span>:<span class="hljs-string"> <span class="hljs-number">16px</span></span></span>; }</span><span class="hljs-regexp">.form</span> <span class="hljs-regexp">.text</span> <span class="hljs-operator">{ <span class="hljs-operator"><span class="hljs-title1">padding</span>:<span class="hljs-string"> <span class="hljs-number">3px</span></span></span>; <span class="hljs-operator"><span class="hljs-title1">margin</span>:<span class="hljs-string"><span class="hljs-number">2px</span> <span class="hljs-number">10px</span></span></span>; <span class="hljs-operator"><span class="hljs-title1">width</span>:<span class="hljs-string"> <span class="hljs-number">240px</span></span></span>; <span class="hljs-operator"><span class="hljs-title1">height</span>:<span class="hljs-string"> <span class="hljs-number">24px</span></span></span>; <span class="hljs-operator"><span class="hljs-title1">line-height</span>:<span class="hljs-string"> <span class="hljs-number">28px</span></span></span>; <span class="hljs-operator"><span class="hljs-title1">border</span>:<span class="hljs-string"> <span class="hljs-number">1px</span> solid <span class="hljs-title">#D4D4D4</span></span></span>; }</span><span class="hljs-regexp">.form</span> <span class="hljs-regexp">.btn</span><span class="hljs-operator">{ <span class="hljs-operator"><span class="hljs-title1">margin</span>:<span class="hljs-string"><span class="hljs-number">6px</span></span></span>; <span class="hljs-operator"><span class="hljs-title1">padding</span>:<span class="hljs-string"> <span class="hljs-number">6px</span></span></span>; <span class="hljs-operator"><span class="hljs-title1">width</span>:<span class="hljs-string"> <span class="hljs-number">120px</span></span></span>; <span class="hljs-operator"><span class="hljs-title1">font-size</span>:<span class="hljs-string"> <span class="hljs-number">16px</span></span></span>; <span class="hljs-operator"><span class="hljs-title1">border</span>:<span class="hljs-string"> <span class="hljs-number">1px</span> solid <span class="hljs-title">#D4D4D4</span></span></span>; <span class="hljs-operator"><span class="hljs-title1">cursor</span>:<span class="hljs-string"> pointer</span></span>; <span class="hljs-operator"><span class="hljs-title1">background</span>:<span class="hljs-string"><span class="hljs-title">#eee</span></span></span>; }</span><span class="hljs-regexp">a</span><span class="hljs-operator">{ <span class="hljs-operator"><span class="hljs-title1">color</span>:<span class="hljs-string"> <span class="hljs-title">#868686</span></span></span>; <span class="hljs-operator"><span class="hljs-title1">cursor</span>:<span class="hljs-string"> pointer</span></span>; }</span><span class="hljs-operator"><span class="hljs-title1">a</span>:<span class="hljs-string">hover{ text-decoration: underline</span></span>; } <span class="hljs-regexp">h2</span><span class="hljs-operator">{ <span class="hljs-operator"><span class="hljs-title1">color</span>:<span class="hljs-string"> <span class="hljs-title">#4288ce</span></span></span>; <span class="hljs-operator"><span class="hljs-title1">font-weight</span>:<span class="hljs-string"> <span class="hljs-number">400</span></span></span>; <span class="hljs-operator"><span class="hljs-title1">padding</span>:<span class="hljs-string"> <span class="hljs-number">6px</span> <span class="hljs-number">0</span></span></span>; <span class="hljs-operator"><span class="hljs-title1">margin</span>:<span class="hljs-string"> <span class="hljs-number">6px</span> <span class="hljs-number">0</span> <span class="hljs-number">0</span></span></span>; <span class="hljs-operator"><span class="hljs-title1">font-size</span>:<span class="hljs-string"> <span class="hljs-number">28px</span></span></span>; <span class="hljs-operator"><span class="hljs-title1">border-bottom</span>:<span class="hljs-string"> <span class="hljs-number">1px</span> solid <span class="hljs-title">#eee</span></span></span>; }</span><span class="hljs-regexp">div</span><span class="hljs-operator">{ <span class="hljs-operator"><span class="hljs-title1">margin</span>:<span class="hljs-string"><span class="hljs-number">8px</span></span></span>; }</span><span class="hljs-regexp">.info</span><span class="hljs-operator">{ <span class="hljs-operator"><span class="hljs-title1">padding</span>:<span class="hljs-string"> <span class="hljs-number">12px</span> <span class="hljs-number">0</span></span></span>; <span class="hljs-operator"><span class="hljs-title1">border-bottom</span>:<span class="hljs-string"> <span class="hljs-number">1px</span> solid <span class="hljs-title">#eee</span></span></span>; }</span><span class="hljs-regexp">.copyright</span><span class="hljs-operator">{ <span class="hljs-operator"><span class="hljs-title1">margin-top</span>:<span class="hljs-string"> <span class="hljs-number">24px</span></span></span>; <span class="hljs-operator"><span class="hljs-title1">padding</span>:<span class="hljs-string"> <span class="hljs-number">12px</span> <span class="hljs-number">0</span></span></span>; <span class="hljs-operator"><span class="hljs-title1">border-top</span>:<span class="hljs-string"> <span class="hljs-number">1px</span> solid <span class="hljs-title">#eee</span></span></span>; }</span></span><span class="hljs-regexp"></<span class="hljs-operator">style</span>></span><span class="hljs-regexp"></<span class="hljs-operator">head</span>></span><span class="hljs-regexp"><<span class="hljs-operator">body</span>></span><span class="hljs-regexp"><<span class="hljs-operator">h2</span>></span>Session示例<span class="hljs-regexp"></<span class="hljs-operator">h2</span>></span><span class="hljs-regexp"><<span class="hljs-operator">FORM</span> <span class="hljs-operator">method</span>=<span class="hljs-string">"post"</span> <span class="hljs-operator">class</span>=<span class="hljs-string">"form"</span> <span class="hljs-operator">action</span>=<span class="hljs-string">"{:url('save')}"</span>></span> 输入用户名:<span class="hljs-regexp"><<span class="hljs-operator">INPUT</span> <span class="hljs-operator">type</span>=<span class="hljs-string">"text"</span> <span class="hljs-operator">class</span>=<span class="hljs-string">"text"</span> <span class="hljs-operator">name</span>=<span class="hljs-string">"name"</span> <span class="hljs-operator">value</span>=<span class="hljs-string">"{$Request.session.user_name}"</span> ></span><span class="hljs-regexp"><<span class="hljs-operator">br</span>/></span><span class="hljs-regexp"><<span class="hljs-operator">INPUT</span> <span class="hljs-operator">type</span>=<span class="hljs-string">"submit"</span> <span class="hljs-operator">class</span>=<span class="hljs-string">"btn"</span> <span class="hljs-operator">value</span>=<span class="hljs-string">" 保存 "</span>></span><span class="hljs-regexp"></<span class="hljs-operator">FORM</span>></span><span class="hljs-regexp"><<span class="hljs-operator">div</span> <span class="hljs-operator">class</span>=<span class="hljs-string">"copyright"</span>></span><span class="hljs-regexp"><<span class="hljs-operator">a</span> <span class="hljs-operator">title</span>=<span class="hljs-string">"官方网站"</span> <span class="hljs-operator">href</span>=<span class="hljs-string">"http://www.thinkphp.cn"</span>></span>ThinkPHP<span class="hljs-regexp"></<span class="hljs-operator">a</span>></span><span class="hljs-regexp"><<span class="hljs-operator">span</span>></span>V5<span class="hljs-regexp"></<span class="hljs-operator">span</span>></span><span class="hljs-regexp"><<span class="hljs-operator">span</span>></span>{ 十年磨一剑-为API开发设计的高性能框架 }<span class="hljs-regexp"></<span class="hljs-operator">span</span>></span><span class="hljs-regexp"></<span class="hljs-operator">div</span>></span><span class="hljs-regexp"></<span class="hljs-operator">body</span>></span><span class="hljs-regexp"></<span class="hljs-operator">html</span>></span> ``` ``` 访问URL地址 ``` <pre class="calibre18"> ``` <span class="hljs-string">http:</span> <span class="hljs-comment">//tp5.com</span> ``` ``` 页面显示: ![](https://img.kancloud.cn/72/a9/72a9358252fa6474c57dead184765658_488x233.png) 当我们输入`ThinkPHP`后点击保存按钮 ![](https://img.kancloud.cn/2a/24/2a24bc6e237eb2435e6b3ce5d0ceb6f3_472x235.png) ![](https://img.kancloud.cn/1b/7a/1b7ac9abe7c49038bf5116d33639b142_392x234.png) 然后页面后 用户名输入框中会显示已经保存的`session`值。 ![](https://img.kancloud.cn/2a/24/2a24bc6e237eb2435e6b3ce5d0ceb6f3_472x235.png)