大家好。
我正在使用cdbhttpsession,去保存用户登陆情况等。
当不使用以下设置时,用户可以正常登陆。。因为默认使用Chttpsession
后来做了以下更改
‘session’ => array(
'class' => 'CDbHttpSession',
'sessionTableName' => 'sessions',
'autoCreateSessionTable' =>false, (之前时true,后来table存在就改回来了)
'connectionID' => 'db',
'cookieParams' => array('domain' => '.' . $current_domain ),
'timeout' => 36000,
'sessionName' => 'SECSESS',
),
当用户登陆时,一切都正常。
Yii:app()->user->login($identity,$duration); ==> 返回 true
Yii:app()->user->getId(); ==> 返回 ex: 2031
当我更换,或者刷新页面时,就丢失了登陆信息。
Yii:app()->user->getId(); ==> 返回 0
并且 session_id() 返回每次得值都不一样。。。
public function getId()
{
return $this->getState('__id') ? $this->getState('__id') : 0;
}
====================
我有创建了一个新的demo app 去测试,也发现了一样得问题。
是不是在 webuser 得处理上有问题?
另外得问题,在数据库里,每一次用户刷新页面,都会自动在数据表中写入行。。。。
求解--谢谢
yiqing95
(Yiqing 95)
April 5, 2013, 5:25am
2
一次不要配那么多参数 先配最基本的 都运行ok时 慢慢加参数
我也存在数据库中 但没那种问题:
'session' => array(
'class' => 'application.my.components.YsDbHttpSession',
'sessionName' => 'yiiSpace',
'connectionID'=>'db', // important !! if not set will use sqliteDb as storage .
// 'gCProbability'=>4 , // every 4 request will invoke the Session GC 测试打开之
// 'timeout'=>120 ,
),
// 自定义session表:
class YsDbHttpSession extends CDbHttpSession
{
/**
* Creates the session DB table.
* @param CDbConnection $db the database connection
* @param string $tableName the name of the table to be created
*/
protected function createSessionTable($db,$tableName)
{
$driver=$db->getDriverName();
if($driver==='mysql')
$blob='LONGBLOB';
else if($driver==='pgsql')
$blob='BYTEA';
else
$blob='BLOB';
$db->createCommand()->createTable($tableName,array(
'id'=>'CHAR(32) PRIMARY KEY',
'expire'=>'integer',
'data'=>$blob,
// 'user_id'=> " int(11) NOT NULL DEFAULT '0' ",
'user_id'=> " int(11) DEFAULT '0' ",
));
}
}
// 主要是添加了一个user_id 字段 用来统计在线人数的
我再也没搞其他的动作了 一切都良好; 你那个感觉要么是每次重建表 要么是session_id 每次都不一样 有可能是cookie设置问题 有个函数 是session重新生成id的 你自己查下 然后用IDE在yii库中搜索下 看什么条件会重新生sessionId 看源代码
yiqing95:
一次不要配那么多参数 先配最基本的 都运行ok时 慢慢加参数
我也存在数据库中 但没那种问题:
'session' => array(
'class' => 'application.my.components.YsDbHttpSession',
'sessionName' => 'yiiSpace',
'connectionID'=>'db', // important !! if not set will use sqliteDb as storage .
// 'gCProbability'=>4 , // every 4 request will invoke the Session GC 测试打开之
// 'timeout'=>120 ,
),
// 自定义session表:
class YsDbHttpSession extends CDbHttpSession
{
/**
* Creates the session DB table.
* @param CDbConnection $db the database connection
* @param string $tableName the name of the table to be created
*/
protected function createSessionTable($db,$tableName)
{
$driver=$db->getDriverName();
if($driver==='mysql')
$blob='LONGBLOB';
else if($driver==='pgsql')
$blob='BYTEA';
else
$blob='BLOB';
$db->createCommand()->createTable($tableName,array(
'id'=>'CHAR(32) PRIMARY KEY',
'expire'=>'integer',
'data'=>$blob,
// 'user_id'=> " int(11) NOT NULL DEFAULT '0' ",
'user_id'=> " int(11) DEFAULT '0' ",
));
}
}
// 主要是添加了一个user_id 字段 用来统计在线人数的
我再也没搞其他的动作了 一切都良好; 你那个感觉要么是每次重建表 要么是session_id 每次都不一样 有可能是cookie设置问题 有个函数 是session重新生成id的 你自己查下 然后用IDE在yii库中搜索下 看什么条件会重新生sessionId 看源代码
用eclipse搜索了一下。 在cdbhttpsession里有重新生成session_id ,我复制粘贴了一下。
public function regenerateID($deleteOldSession=false)
{
$oldID=session_id();
// if no session is started, there is nothing to regenerate
if(empty($oldID))
return;
parent::regenerateID(false);
$newID=session_id();
$db=$this->getDbConnection();
$row=$db->createCommand()
->select()
->from($this->sessionTableName)
->where(‘id=:id’,array(’:id’=>$oldID))
->queryRow();
if($row!==false)
{
if($deleteOldSession)
$db->createCommand()->update($this->sessionTableName,array(
‘id’=>$newID
),‘id=:oldID’,array(’:oldID’=>$oldID));
else
{
$row[‘id’]=$newID;
$db->createCommand()->insert($this->sessionTableName, $row);
}
}
else
{
// shouldn’t reach here normally
$db->createCommand()->insert($this->sessionTableName, array(
‘id’=>$newID,
‘expire’=>time()+$this->getTimeout(),
));
}
}
谢谢你得帮助,我发现了是cookie参数设置得问题。