在LoginForm中:
在UserIndentify中:
在登录的视图文件中:
记住用户登录状态:
1、在登陆方法中
if(isset($_POST['LoginForm']))
{
$model->attributes=$_POST['LoginForm'];
// validate user input and redirect to the previous page if valid
if($model->validate() && $model->login())
$this->redirect(Yii::app()->user->returnUrl);
}
其中的login是记录了用户对应的session和cookie信息,这些信息是从validate()方法中来的,login方法如下:
public function login()
{
if($this->_identity===null)
{
$this->_identity=new UserIdentity($this->username,$this->password);
$this->_identity->authenticate();
}
if($this->_identity->errorCode===UserIdentity::ERROR_NONE)
{
$duration=$this->rememberMe ? 3600*24*30 : 0; // 30 days
Yii::app()->user->login($this->_identity,$duration);
return true;
}
else
return false;
}
其中的$this->_identity就是获得的用户信息。默认只是将用户名写入了session,可以重写他的方法将其他的写入session,如下在UserIdentity.php中:
private $_id;
/*
* 进行用户名和密码的验证
*/
public function authenticate()
{
$username=strtolower($this->username);
$user=User::model()->find('LOWER(username)=?',array($username));
if($user===null)
$this->errorCode=self::ERROR_USERNAME_INVALID;
else if(!UserSalt::vertifySalt($this->username,$this->password))
$this->errorCode=self::ERROR_PASSWORD_INVALID;
else
{
$this->_id=$user->userid;
$this->username=$user->username;
$this->errorCode=self::ERROR_NONE;
}
return $this->errorCode==self::ERROR_NONE;
}
/*
* 重写方法,用户获取用户的id,因为默认的是获取用户的名称
*/
public function getId()
{
return $this->_id;
}
/*
* 重写方法,将除了用户名和用户ID的其他内容写入session
*/
public function getPersistentStates(){
return array('zhouzhuang','yiduokeji');
}
这样就可以将最后的zhouzhuang,yiduokeji也写入session中。
可以通过Yii::app()->user->id,Yii::app()->user->name来进行访问。
转载于:https://blog.51cto.com/dreamingo/1321290