基本设置方案见:http://www.linuxidc.com/Linux/2014-11/109283.htm
用Yii开发时,通过上述方式并不能在多个子域名间达到预期,因为通过Yii::app()->user设置的session,Yii框架会自动在key的前面添加"_keyPrefix"
CWebUser.php
public function getState($key,$defaultValue=null)
{
$key=$this->getStateKeyPrefix().$key;
return isset($_SESSION[$key]) ? $_SESSION[$key] : $defaultValue;
}
public function getStateKeyPrefix()
{
if($this->_keyPrefix!==null){
return $this->_keyPrefix;
}else{
return $this->_keyPrefix=md5(‘Yii.‘.get_class($this).‘.‘.Yii::app()->getId());
}
}CApplication.php
public function getId()
{
if($this->_id!==null)
return $this->_id;
else
return $this->_id=sprintf(‘%x‘,crc32($this->getBasePath().$this->name));
}因为默认情况各子域名的路径和项目名不同,导致在A域名下登录,而在B子域名下无法识别己登录。
解决方案:
所有子域名项目中的main.php文件中设置相同的
return array( ‘id‘=>‘peter.com‘, ....... );
或
return array( .......... ‘components‘=>array( ‘user‘=>array( // enable cookie-based authentication ‘allowAutoLogin‘=>true, ‘stateKeyPrefix‘=>‘peter.com‘ ), ..... ) );
本文出自 “peter的未来” 博客,请务必保留此出处http://peterxu.blog.51cto.com/1391852/1636620
原文地址:http://peterxu.blog.51cto.com/1391852/1636620