我目前正在开展一个项目,我开始构建这个项目的其他人。 当我第一次启动时,项目是两个不同的子域x.domain.com和y.domain.com 现在我们正在将其转移到一个域,但有两个站点domain.com/x和domain.com/y。 之前登录功能仅在x子域上可用,但现在我希望用户也能够在两个站点上登录。
每个站点都有主控制器(xController和yController),它们都扩展了xyController。
如果我登录x网站,一切顺利,但一旦我去domian.com/y
yii:app()->user->isGuest returns true
如果我回到domain.com/x即时登录。
我无法弄清楚为什么会发生这种情况PHPSESSID cookie对于两个网站都是一样的。
这是我的课程,扩展了CUserIdentity:
class UserIdentity extends CUserIdentity{
private $_id;
public function authenticate(){
$user=User::model()->findByAttributes(array('user_name'=>$this->username));
if($user === null){
$this->errorCode=self::ERROR_USERNAME_INVALID;
}else{
if($this->comparePassword($user, $this->password)){
/** Do some other checks here **/
$this->_id=$user->id;
$this->errorCode=self::ERROR_NONE;
return !$this->errorCode;
}
$this->errorCode=self::ERROR_USERNAME_INVALID;
}
return !$this->errorCode;
}
public function getId(){
return $this->_id;
}
}
,这是配置文件中的部分
'components'=>array(
'user'=>array(
'class'=>'WebUser',
// enable cookie-based authentication
'allowAutoLogin'=>true,
),
...
[编辑]我发现问题是什么,我必须在配置数组中将id选项设置为两个配置中的相同值,之前没有设置id
答案 0 :(得分:0)
在您的配置中启用基于Cookie的身份验证:
'user' => array(
'allowAutoLogin' => true,
),
配置会话组件:
'session' => array(
'savePath' => '/some/writeable/path',
'cookieMode' => 'allow',
'cookieParams' => array(
'path' => '/',
'domain' => '.yourdomain.com',
'httpOnly' => true,
),
),
确保 / some / writeable / path 真的可写!
最后,这是Yii的关键部分(上面的cookie配置是通用PHP),必须在配置文件中设置Yii应用程序ID:
'id' => 'yourdomain',
就是这样!