2014-10-31 89 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