yii2 session的使用方法

WBOY
发布: 2016-08-08 09:32:32
原创
1324 人浏览过
yii2打开session<span></span>
<span>use</span><span>yii</span><span>\</span><span>web</span><span>\</span><span>Session</span><span>;
 
</span><span>$session</span><span> = </span><span>Yii</span><span>::</span><span>$app</span><span>-></span><span>session</span><span>;
</span><span>//</span><span> check if a session is already open</span><span>if</span><span>(</span><span>$session</span><span>-></span><span>isActive</span><span>)</span><span> ...
</span><span>//</span><span> open a session</span><span>$session</span><span>-></span><span>open</span><span>(</span><span>)</span><span>;
</span><span>//</span><span> close a session</span><span>$session</span><span>-></span><span>close</span><span>(</span><span>)</span><span>;
</span><span>//</span><span> destroys all data registered to a session.</span><span>$session</span><span>-></span><span>destroy</span><span>(</span><span>)</span><span>;

</span><span>设置</span><span>session
</span><span>$session</span><span> = </span><span>Yii</span><span>::</span><span>$app</span><span>-></span><span>session</span><span>;
</span><span>$session</span><span>-></span><span>set</span><span>(</span><span>'</span><span>user_id</span><span>'</span><span>, </span><span>'</span><span>1234</span><span>'</span><span>)</span><span>;
</span><span>//</span><span>OR</span><span>$session</span><span>[</span><span>'</span><span>user_id</span><span>'</span><span>]</span><span> = </span><span>'</span><span>1234</span><span>'</span><span>;
</span><span>//</span><span>OR</span><span>$_SESSION</span><span>[</span><span>'</span><span>user_id</span><span>'</span><span>]</span><span> = </span><span>'</span><span>1234</span><span>'</span><span>;

</span><span>读取</span><span>session
</span><span>$session</span><span> = </span><span>Yii</span><span>::</span><span>$app</span><span>-></span><span>session</span><span>;</span>
登录后复制
<span></span>
登录后复制
登录后复制
<span>$user_id</span><span> = </span><span>$session</span><span>-></span><span>get</span><span>(</span><span>'</span><span>user_id</span><span>'</span><span>)</span><span>;
</span><span>//</span><span>OR</span><span>$user_id</span><span> = </span><span>$session</span><span>[</span><span>'</span><span>user_id</span><span>'</span><span>]</span><span>;
</span><span>//</span><span>OR</span><span>$user_id</span><span> = </span><span>isset</span><span>(</span><span>$_SESSION</span><span>[</span><span>'</span><span>user_id</span><span>'</span><span>]</span><span>)</span><span> ? </span><span>$_SESSION</span><span>[</span><span>'</span><span>user_id</span><span>'</span><span>]</span><span> : </span><span>null</span><span>;
销毁</span><span>session</span><span>$session</span><span> = </span><span>Yii</span><span>::</span><span>$app</span><span>-></span><span>session</span><span>;
</span><span>$session</span><span>-></span><span>remove</span><span>(</span><span>'</span><span>user_id</span><span>'</span><span>)</span><span>;
</span><span>//</span><span>OR</span><span>unset</span><span>(</span><span>$session</span><span>[</span><span>'</span><span>user_id</span><span>'</span><span>]</span><span>)</span><span>;
</span><span>//</span><span>OR</span><span>unset</span><span>(</span><span>$_SESSION</span><span>[</span><span>'</span><span>user_id</span><span>'</span><span>]</span><span>)</span><span>;

</span><span>数组</span><span>session</span><span>的读取与设置

</span><span>$session</span><span> = </span><span>Yii</span><span>::</span><span>$app</span><span>-></span><span>session</span><span>;
</span><span>$session</span><span>[</span><span>'</span><span>user</span><span>'</span><span>]</span><span> = </span><span>[</span><span>'</span><span>id</span><span>'</span><span> => </span><span>1</span><span>,
    </span><span>'</span><span>username</span><span>'</span><span> => </span><span>'</span><span>yiiuser</span><span>'</span><span>,
</span><span>]</span><span>;
</span><span>echo</span><span>$session</span><span>[</span><span>'</span><span>user</span><span>'</span><span>]</span><span>[</span><span>'</span><span>id</span><span>'</span><span>]</span><span>;
</span><span>echo</span><span>$session</span><span>[</span><span>'</span><span>user</span><span>'</span><span>]</span><span>[</span><span>'</span><span>username</span><span>'</span><span>]</span><span>;
 
</span><span>$session</span><span>[</span><span>'</span><span>user.id</span><span>'</span><span>]</span><span> = </span><span>1</span><span>;
</span><span>$session</span><span>[</span><span>'</span><span>user.username</span><span>'</span><span>]</span><span> = </span><span>'</span><span>yiiuser</span><span>'</span><span>;</span>
登录后复制
<span></span>
登录后复制
登录后复制
<span>转自:http://www.kuitao8.com/20141213/3351.shtml</span>
登录后复制

以上就介绍了yii2 session的使用方法,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板