如何更改登录用户的密码

时间:2017-07-27 17:59:10

标签: php codeigniter change-password

是否有任何解决方案如何为登录用户更改密码?我想为登录用户创建一个更改密码,我所做的代码只更改用户ID为1的用户密码。对于登录用户,它不会更改。所以任何想法?

这是我的控制者:

    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); }

1 个答案:

答案 0 :(得分:0)

当有人登录时,您必须设置$_SESSION来存储您的用户信息,例如用户名,ID,电子邮件......,以便在需要时使用。然后,您可以检查$_SESSION['session_name']['id']以更改用户密码。 $_SESSION很重要,必须在用户登录时设置。

相关问题