12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- <?php
- namespace app\common\helpers;
- use Yii;
- /*
- * Session.php
- * Session 操作
- * 更新: Jacky.Chen 2019/06/20 15:45
- *
- */
- class Session
- {
- static function getInitCls()
- {
- static $initCls = NULL;
- if(!isset($initCls)) $initCls = new Session();
- return $initCls;
- }
- //设置
- public static function set($name,$value)
- {
- $session = Yii::$app->session;
- if ($session->isActive)$session->open();
- if(strpos($name,'.')!==false)
- {
- self::setArray($name,$value);
- }
- else{
- $session->set($name, $value);
- }
- return true;
- }
- //读取
- public static function get($name)
- {
- $session = Yii::$app->session;
- if ($session->isActive)$session->open();
- if(strpos($name,'.')!==false)
- {
- return self::getArray($name);
- }
- else
- {
- return $session->get($name);
- }
- }
- //设置数组中的值,用法 setArray('user.name','jack')
- public static function setArray($name,$value)
- {
- $session = Yii::$app->session;
- if ($session->isActive)$session->open();
- $names = explode('.',$name);
- $rs = $session->get($names[0]);
- $rs[$names[1]] = $value;
- self::set($names[0],$rs);
- return true;
- }
- //读取
- public static function getArray($name)
- {
- $session = Yii::$app->session;
- if ($session->isActive)$session->open();
- $names = explode('.',$name);
- return $session[$names[0]][$names[1]];
- }
- //删除
- public static function remove($name)
- {
- $session = Yii::$app->session;
- if ($session->isActive)$session->open();
- $session->remove($name);
- return true;
- }
- //销毁
- public static function destory()
- {
- $session = Yii::$app->session;
- if ($session->isActive)$session->open();
- $session->destroy();
- return true;
- }
- }
|