问题
Hi i am new in yii and below is my UserIdentiy function Please let me know how can i add the remember me functionality
public function authenticate()
{
$users = array();
if ($this->usertype == "registration")
{
$users = Login::model()->findByAttributes(array('email' => $this->username));
$users = $users->attributes;
}
if (empty($users)) $this->errorCode = self::ERROR_USERNAME_INVALID;
elseif (!empty($users['password']) && $users['password'] !== md5($this->password))
$this->errorCode = self::ERROR_PASSWORD_INVALID;
elseif (!empty($users['status']) && $users['status'] !== 1)
$this->errorCode = self::STATUS_NOT_ACTIVE;
else
{
$this->_id = $users->id;
$this->errorCode = self::ERROR_NONE;
}
return !$this->errorCode;
}
回答1:
In protected\config\main.php
configuration array is present in that array go to component
index. Inside of that user
array has associative indexed value 'allowAutoLogin'
must have the boolean value true
So it should look like this
'components' => array(
'user' => array(
// enable cookie-based authentication
'allowAutoLogin' => true,
),
...
And You have to use the following property along with login method given below you can achieve remember me easily.
class LoginForm extends CFormModel
{
public $username;
public $password;
public $rememberMe;
private $_identity;
login method should be like this in Login model class
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;
}
And this the core code to remember function
Yii::app()->user->login($this->_identity,$duration);
来源:https://stackoverflow.com/questions/26674611/yii-remember-me-functionality