Yii Remember me functionality?

痴心易碎 提交于 2020-01-05 02:52:11

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!