2014-04-30 147 views
0

我是Yii PHP Framework的新手,我試圖在登錄表單上工作。我知道在安裝測試驅動器應用程序時,Yii上已經有了一個登錄功能。我只是編輯它通過數據庫登錄,它不工作。我在代碼中沒有任何錯誤,但是當我登錄時總是說錯誤的密碼或用戶名。這是我的代碼。Yii登錄不起作用

對於UserIdentity.php

class UserIdentity extends CUserIdentity 
{ 
/** 
* Authenticates a user. 
* The example implementation makes sure if the username and password 
* are both 'demo'. 
* In practical applications, this should be changed to authenticate 
* against some persistent user identity storage (e.g. database). 
* @return boolean whether authentication succeeds. 
*/ 
    private $_id; 
    public function authenticate() 
    { 
     $record=User::model()->findByAttributes(array('username'=>$this->username)); 
     if($record===null) 
      $this->errorCode=self::ERROR_USERNAME_INVALID; 
     else if($record->password!==crypt($this->password,$record->password)) 
      $this->errorCode=self::ERROR_PASSWORD_INVALID; 
     else 
     { 
      $this->_id=$record->id; 
      $this->setState('title', $record->title); 
      $this->errorCode=self::ERROR_NONE; 
     } 
     return !$this->errorCode; 
    } 

    public function getId() 
    { 
    return $this->_id; 
    } 
} 

下面是SiteController.php

public function actionLogin() 
{ 
    $model=new LoginForm; 

    // if it is ajax validation request 
    if(isset($_POST['ajax']) && $_POST['ajax']==='login-form') 
    { 
     echo CActiveForm::validate($model); 
     Yii::app()->end(); 
    } 

    // collect user input data 
    if(isset($_POST['LoginForm'])) 
    { 
     $model->attributes=$_POST['LoginForm']; 
     // validate user input and redirect to the previous page if valid 
     if($model->validate() && $model->login()) 
      $this->redirect(Yii::app()->user->returnUrl); 
    } 
    // display the login form 
    $this->render('login',array('model'=>$model)); 
} 

你能幫助我嗎?謝謝!

+0

你是如何在db中保存密碼的?你有加密嗎? – Hearaman

+0

編號密碼爲純文本。我只是執行mysql插入命令。我做錯了嗎? – Yassi

回答

0

,如果你的密碼保存在數據庫文本純不使用隱窩

class UserIdentity extends CUserIdentity 
{ 
/** 
* Authenticates a user. 
* The example implementation makes sure if the username and password 
* are both 'demo'. 
* In practical applications, this should be changed to authenticate 
* against some persistent user identity storage (e.g. database). 
* @return boolean whether authentication succeeds. 
*/ 
    private $_id; 
    public function authenticate() 
    { 
     $record=User::model()->findByAttributes(array('username'=>$this->username)); 
     if($record===null) 
      $this->errorCode=self::ERROR_USERNAME_INVALID; 
     else if($record->password!==$this->password) // changed 
      $this->errorCode=self::ERROR_PASSWORD_INVALID; 
     else 
     { 
      $this->_id=$record->id; 
      $this->setState('title', $record->title); 
      $this->errorCode=self::ERROR_NONE; 
     } 
     return !$this->errorCode; 
    } 

    public function getId() 
    { 
    return $this->_id; 
    } 
} 
+0

我試過了,但我出了一個錯誤。 屬性「User.title」未定義。 – Yassi

+0

好吧,這意味着登錄查詢運行良好,現在我將編輯我的題目問題的答案。 –

+0

請確保您的用戶表中有標題列,否則請設置public $ title;到您的用戶模型類的頂部 –

0

在您的編碼,該行

else if($record->password!==crypt($this->password,$record->password)) 

是比較字符串散列密碼(crypt(),使串散列/ encription )。

如果保存在數據庫中的密碼沒有加密,你可以比較兩個用戶輸入密碼,並已保存在數據庫中的密碼直接,無需申請crypt()

  ($this->password!==$record->password) 
      //$this->password - user input 
      //$record->password - save password from db 

現在改變代碼

 public function authenticate() 
     { 
      $record = User::model()->findByAttributes(array('username' => $this->username)); 
      if ($record === null) 
       $this->errorCode = self::ERROR_USERNAME_INVALID; 
      else if (trim($this->password)!== $record->password) 
       $this->errorCode = self::ERROR_PASSWORD_INVALID; 
      else 
      { 
       $this->_id = $record->id; 
       $this->setState('title', $record->title); 
       $this->errorCode = self::ERROR_NONE; 
      } 
      return !$this->errorCode; 
     }