简体   繁体   中英

Password Authentification does not working

Hi there this is totally a very basic question but i am new to yii framework and php. I have customized the UserIdentity.php like this

private $_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

 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(). and make sure when you are adding new user the password should be encrypted using 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));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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