2014-10-31 69 views
2

嗨,我在警予是新的,下面是我的UserIdentiy功能,请让我知道我怎样才能添加记得我的功能Yii记住我的功能?

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

你检查附带Yii框架的演示?博客演示在登录页面上具有此功能。 – Jerome 2014-10-31 12:46:18

+0

嘿,我尝试过,但它不工作 – 2014-10-31 12:54:43

回答

3

protected\config\main.php配置阵列存在数组中去component指数。里面的那个user阵列具有关联索引值'allowAutoLogin'必须具有布尔值true

因此,它应该是这样的

'components' => array(
    'user' => array(
     // enable cookie-based authentication 
     'allowAutoLogin' => true, 
    ), 
... 

,你必须给定下面你可以实现登录方法一起使用下面的属性记住我轻松。

class LoginForm extends CFormModel 
{ 
    public $username; 
    public $password; 
    public $rememberMe; 

    private $_identity; 

登录方法应该是这样的登录模型类

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; 
} 

而这个核心代码记忆功能

Yii::app()->user->login($this->_identity,$duration); 
+0

嗨,布尔值已经设置为true。 – 2014-10-31 13:26:12

+0

你会发布你登录模型类 – gvgvgvijayan 2014-10-31 13:41:43