Heim  >  Artikel  >  Backend-Entwicklung  >  So verwenden Sie die yii2-Sitzung

So verwenden Sie die yii2-Sitzung

WBOY
WBOYOriginal
2016-08-08 09:32:321327Durchsuche
yii2Offene Sitzung<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>

Das Obige stellt die Verwendung der yii2-Sitzung vor, einschließlich der relevanten Inhalte. Ich hoffe, dass es für Freunde hilfreich ist, die sich für PHP-Tutorials interessieren.

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn