首頁  >  文章  >  後端開發  >  yii2 session的使用方法

yii2 session的使用方法

WBOY
WBOY原創
2016-08-08 09:32:321326瀏覽
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教程有興趣的朋友有所幫助。

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn