2014-04-30 5 views
0

저는 Yii PHP Framework를 처음 사용하고 있으며 로그인 양식 작업을하고 있습니다. 나는 이미 Yii에 testdrive 앱을 설치할 때 로그인 기능이 있다는 것을 알고 있습니다. 나는 데이터베이스를 통해 로그인하도록 편집하고 작동하지 않습니다. 코드에 오류가 없지만 로그인 할 때 항상 잘못된 비밀번호 또는 사용자 이름이 표시됩니다. 여기 내 코드가있다. 여기 UserIdentity.phpYii 로그인이 작동하지 않습니다.

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

를 들어

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

당신이 나를 도울 수 SiteController.php

위한거야? 감사! 비밀번호는 DB를 텍스트로 저장 한 경우

+0

어떻게 암호를 db로 저장 했습니까? 암호화 했니? – Hearaman

+0

아니요. 암호는 일반 텍스트입니다. 방금 mysql insert 명령을 실행했습니다. 내가 잘못 했니? – 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

ok 그것은 로그인 쿼리가 잘 실행되고 있음을 의미합니다. 이제는 제목 문제에 대한 답을 편집 할 것입니다. –

+0

사용자 테이블에 title 열이 있는지 확인하고, 그렇지 않으면 public $ title을 설정하십시오. User 모델 클래스의 맨 위로 –

0

,

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

문자열의 해시와 암호 ( crypt()을 비교하는 라인 문자열 해시/encription하게를 사용하지 말아). 당신은 암호화하지 않고 DB에 암호를 저장하는 경우

, 직접, 필요 지금 crypt()

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

을 적용하지

으로 코드를 변경하기 위해 사용자가 입력 한 암호를 모두 당신이 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; 
     } 
관련 문제