<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>
上記は、関連コンテンツを含めて yii2 セッションの使用方法を紹介したもので、PHP チュートリアルに興味のある友人に役立つことを願っています。