简体   繁体   English

密码验证不起作用

[英]Password Authentification does not working

Hi there this is totally a very basic question but i am new to yii framework and php. 嗨,这是一个非常基本的问题,但是我是yii framework和php的新手。 I have customized the UserIdentity.php like this 我已经像这样自定义了UserIdentity.php

private $_id; 私人$ _id;

 public function authenticate() { $username=strtolower($this->username); $user= User::model()->find('Lower(username)=?',array($username)); if($user===NULL) $this->errorCode=self::ERROR_USERNAME_INVALID; else if($user->validatePassword($this->password)) $this->errorcode=self::ERROR_PASSWORD_INVALID; else { $this->_id=$user->id; $this->username=$user->username; $this->errorCode=self::ERROR_NONE; } return $this->errorCode=self::ERROR_NONE ; /* $users=array( // username => password 'demo'=>'demo', 'admin'=>'admin', ); if(!isset($users[$this->username])) $this->errorCode=self::ERROR_USERNAME_INVALID; elseif($users[$this->username]!==$this->password) $this->errorCode=self::ERROR_PASSWORD_INVALID; else $this->errorCode=self::ERROR_NONE; return !$this->errorCode; */ } 

and in User.php model i write like this User.php模型中,我这样写

 public function validatePassword($password)
        {
            return $this->hashPassword($password)===$this->password;
        }
        public function hashPassword($password)
        {
            return md5($password) ;
        }

but when i type password it does not authenticate, normally the username and password is demo but i cant figure it out that whats going wrong here. 但是,当我键入密码时,它不进行身份验证,通常用户名和密码为演示,但我无法弄清楚这里出了什么问题。 Please bear me as a beginner. 请以我为初学者。

Change your code like this... 像这样更改您的代码...

else if(!$user->validatePassword($this->password))
        $this->errorcode=self::ERROR_PASSWORD_INVALID;

there should be ! 应该有 with else if(). 与其他if()。 and make sure when you are adding new user the password should be encrypted using md5() . 并确保在添加新用户时,应使用md5()对密码进行加密。 that also just check it out in db.. 那也只需在数据库中检查一下即可。

I think you have problem in this line... 我认为您在这方面有问题...

 $user=  User::model()->find('Lower(username)=?',array($username));

try to change it with this 尝试用这个改变它

$user=  User::model()->findByAttributes(array('username'=>$username));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM