YII session

来源:互联网 发布:免费视频相册制作软件 编辑:程序博客网 时间:2024/05/06 19:06

CWebUser将用户信息存储在$_SESSION中间,但是尚不清楚这是CHttpSession还是PHP自带的session

打印Yii中的session:
Array( [username] => hello [647e007502b4976ad118e3560af5efd5__id] => admin@qq.com [647e007502b4976ad118e3560af5efd5__name] => admin@qq.com [647e007502b4976ad118e3560af5efd5userId] => 1 [647e007502b4976ad118e3560af5efd5geohash] => wqj6 [647e007502b4976ad118e3560af5efd5latitude] => 34.196306 [647e007502b4976ad118e3560af5efd5longitude] => 108.967506 [647e007502b4976ad118e3560af5efd5refreshTime] => 1409924249 [647e007502b4976ad118e3560af5efd5currentCity] => 西安 [647e007502b4976ad118e3560af5efd5status] => 0 [647e007502b4976ad118e3560af5efd5ago] => 1 [647e007502b4976ad118e3560af5efd5residence] => 西安 [647e007502b4976ad118e3560af5efd5avatar0] => 683ab2982d5748233a45ea77cd0d8f38.jpg [647e007502b4976ad118e3560af5efd5__states] => Array ( [userId] => 1 [geohash] => 1 [latitude] => 1 [longitude] => 1 [refreshTime] => 1 [currentCity] => 1 [status] => 1 [ago] => 1 [residence] => 1 [avatar0] => 1 ))

可以看见有很多前缀,这些session设置在函数
public function setState($key,$value,$defaultValue=null)
{
$key=$this->getStateKeyPrefix().$key;
if($value===$defaultValue)
unset($_SESSION[$key]);
else
$_SESSION[$key]=$value;
}

有一个getStateKeyPrefix()函数:

public function getStateKeyPrefix()
{
if($this->_keyPrefix!==null)
return $this->_keyPrefix;
else
return $this->_keyPrefix=md5('Yii.'.get_class($this).'.'.Yii::app()->getId());
}

说明前app的id的md5值

Yii::app()指的是CwebApplication

GetId方法在CApplication中:

public function getId()
{
if($this->_id!==null)
return $this->_id;
else
return $this->_id=sprintf('%x',crc32($this->getBasePath().$this->name));
}

Yii::app()->user->getStateKeyPrefix();

$name就是漫游人。
Basepath()就是:
C:\xampp\htdocs\ManYouRen\0813\protected

因为yii::app()->session肯定是CHttpSession

可以看出CwebUser中的session使用的是PHP自带的Session。
其session_start是在CHttpSession中的open方法中。

观察

public function getState($key,$defaultValue=null)
{
$key=$this->getStateKeyPrefix().$key;
return isset($_SESSION[$key]) ? $_SESSION[$key] : $defaultValue;
}

Key是将其组合起来了。

YII的 session:
$key= Yii::app()->user-> getStateKeyPrefix();
p(Yii::app()->session[$key.'userId']);

这样就能获取值了。说明这两个session其实是一样的。只不过YII webuser中的session多了一个前缀。

如果我们自定义了httpsession,那么webuser中的session自然就发生了变化。

http://www.yiiframework.com/forum/index.php/topic/137-cdbhttpsession/page__p__673__hl__db+e+ion

'session' => array(
'timeout' => 36000,
'class'=>'CDbHttpSession',

    ) :

如果是这样的话,
默认的DBHttpSession
CDbHttpSession Object( [connectionID] => [sessionTableName] => YiiSession [autoCreateSessionTable] => 1 [_db:CDbHttpSession:private] => CDbConnection Object ( [connectionString] => sqlite:C:\xampp\htdocs\phpstorm\study\protected\runtime\session-1.1.16-dev.db [username] => [password] =>

是存储在sqlite里面的

要存入mysql:
'session' => array(
'timeout' => 36000,
'class' => 'CDbHttpSession',
'connectionID' => 'db',

    ) ,

之所以 'connectionID' => 'db',是因为main配置文件中有:
'db' => require (dirname(FILE) . DIRECTORY_SEPARATOR . 'database.php') ,
就是使用配置的数据库:

打印出来的值说明了一切。

CDbHttpSession Object( [connectionID] => db [sessionTableName] => YiiSession [autoCreateSessionTable] => 1 [_db:CDbHttpSession:private] => CDbConnection Object ( [connectionString] => mysql:host=localhost;port=3306;dbname=manyouren [username] => root [password] => [schemaCachingDuration] => 0 [schemaCachingExclude] => Array ( )

数据库的存储值用的是二进制对象。这样就不需要改表了!!

Expire是过期时间。我们在session中设置了timeout

但是退出登录的话并没有清空session!!
数据库的session并没有清空。
但是再次登录session不会产生一个值,而是直接在原来的基础上变化。

不管是否登录。Session总是存在的。
登录只不过是新建了一个session而已。
用手机浏览,如果不登录是不会增加新的session的。
但是浏览网页,只要到首页,就会增加一个session呀!

0 0
原创粉丝点击