我想问如何更改登录用户的密码, 当我输入与数据库中的任何人相匹配的密码时,我可以更改密码。 仅举例,用户有“admin”密码,我只需输入当前密码, 新密码并确认密码。Codeigniter:更改登录用户的密码和使用md5的密码
当前密码:admin 新密码:newadmin 当前密码:新的管理
同时,我不知道如何更改密码,如果密码 使用MD5()。我希望你能帮助我,我是Codeigniter的新手。 我搜索的答案,但我真的不明白它,所以我想评论,但 它需要50声望,所以我发布了新的问题。
这里是我的代码:
控制器
public function update(){
$this->form_validation->set_rules('password', 'Current Password', 'required|alpha_numeric|min_length[6]|max_length[20]');
$this->form_validation->set_rules('newpass', 'New Password', 'required|alpha_numeric|min_length[6]|max_length[20]');
$this->form_validation->set_rules('confpassword', 'Confirm Password', 'required|alpha_numeric|min_length[6]|max_length[20]');
if($this->form_validation->run()){
$cur_password = $this->input->post('password');
$new_password = $this->input->post('newpass');
$conf_password = $this->input->post('confpassword');
$this->load->model('queries');
$userid = '1';
$passwd = $this->queries->getCurrPassword($userid);
if($passwd->password == $cur_password){
if($new_password == $conf_password){
if($this->queries->updatePassword($new_password, $userid)){
echo 'Password updated successfully';
}
else{
echo 'Failed to update password';
}
}
else{
echo 'New password & Confirm password is not matching';
}
}
else{
echo'Sorry! Current password is not matching';
}
}
else{
echo validation_errors();
}
模式
public function getCurrPassword($userid){
$query = $this->db->where(['id'=>$userid])
->get('users');
if($query->num_rows() > 0){
return $query->row();
} }
public function updatePassword($new_password, $userid){
$data = array(
'password'=> $new_password
);
return $this->db->where('id', $userid)
->update('users', $data); }
是你的密码数据存储为MD5在你的数据库? – sintakonte
@sintakonte是的。 – squaredsquared
使用PHP使用'password_hash'和'password_verify'。讨论:仅使用散列函数保存密码验证者是不够的,仅仅添加盐对提高安全性没有多大作用。相反,用随机盐迭代HMAC约100ms持续时间,然后用散列表保存盐。使用诸如PBKDF2,Rfc2898DeriveBytes,password_hash,Bcrypt,passlib.hash或类似函数的函数。关键是要让攻击者花费大量时间通过暴力破解密码。 – zaph