问题
We have the following function in /rpotected/components/UserIdentity.php:
public function authenticate()
{
$username = $this->username;
$password = $this->password;
$user = Users::model()->find('username=? AND password=?', array($username, $password));
if($user === NULL){
$this->errorCode=self::ERROR_UNKNOWN_IDENTITY;
}else{
$this->username = $user->username;
sess('SESS_USER_INFO', $user->attributes);
//print_r(sess('SESS_USER_INFO'));
$this->errorCode=self::ERROR_NONE;
}
return !$this->errorCode;
}
Here's a snippet from /protected/models/Users.php:
public function login()
{
if($this->_identity===null)
{
$username = $this->username;
$password = md5($this->password);
//echo "Username: ".$username."<br />Password:".$password;
$this->_identity=new UserIdentity($username, $password);
$this->_identity->authenticate();
}
if($this->_identity->errorCode===UserIdentity::ERROR_NONE)
{
$duration=$this->rememberMe ? 3600*24*30 : 60*20; // 30 days
//print_r($this->_identity);
Yii::app()->user->login($this->_identity,$duration);
//echo "Login Successful";
return true;
}
else{
//echo "Error";
$this->addError('password','Incorrect username or password.');
return false;
}
Problem: Upon login, clicking on My Profiles link prompts for login again. Thus, it seems that the session is not storing/saving the login credentials and carrying them through the login lifespan.
How should the authenticate function be modified so that the session information is stored and so that the login credentials are carried forward?
回答1:
please check "session.cookie_lifetime" value in php.ini
session.cookie_lifetime = 2592000
回答2:
You can store login credentials in a variable of CWebUser class through this way $this->setState(loogedInUser,$user); using setState function.
This information is stored in cookies, not is session and you can access it using Yii::app()->user->loogedInUser at any where.
来源:https://stackoverflow.com/questions/11750033/yii-framework-session-not-saved-after-login