2014-04-30 128 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; 
     }