Home >Backend Development >PHP Tutorial >How to use yii2 session

How to use yii2 session

WBOY
WBOYOriginal
2016-08-08 09:32:321352browse
yii2opensession<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>

The above introduces how to use yii2 session, including various aspects. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn