简体   繁体   中英

Fetching (not decrypt) md5 password from database

I've been working on a password setting where whenever user want to update their password, they cannot update the new password to be the same as the previous/current password. The password store in database is in md5 format.

So what I'm trying to do is converting first the new input password to md5 and compare it to one in the database. This might be a minor error, but after hours of trying and googling, I still cannot fetch the password in the database to do comparison.

I was thinking it might be the parameter. I'm a newbie and still learning, sorry if this was too simple for experts like guys. Thank you in advance for you time.

This is my model

public function getUserPassword($uid) {
    $conditions = array('users.uid="'.$uid.'"');
    $result = $this->find('all', array('conditions' => $conditions,'fields' => array('users.upwd')));
    return $result;    
}

Controller

    $data = $this->request->data;
    $uid = $data['uid'];
    $new_pwd ='';
   if($continue == true) {
        if(md5($new_pwd) == $this->users->getUserPassword($uid)) {
            $continue = false;
            $return_msg = "New password cannot be the same as the old password";            
        }
    }

View

<tr>
    <td><input id="new_pwd" type="password" name="new_password"></input></td>
</tr>

The problem was the way you are fetching the data.It will return as $result['Model']['fieldsname'] . So at the time of comparison you have to take care of that.Change the function getUserPassword() -

public function getUserPassword($uid) {
    $result = $this->field('users.upwd', array('users.uid' => $uid)); //will return the value in db field
    return $result;    
}

And in controller -

$data = $this->request->data;
$uid = $data['uid'];
$new_pwd ='';
if($continue == true) {
    if(md5($new_pwd) == $this->users->getUserPassword($uid)) {
        $continue = false;
        $return_msg = "New password cannot be the same as the old password";            
    }
}

the $new_pwd should be the new_password field in the request data.

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