Php Codeigniter 3应用程序错误:无法将密码与密码\u哈希匹配

Php Codeigniter 3应用程序错误:无法将密码与密码\u哈希匹配,php,codeigniter,Php,Codeigniter,我正在Codeigniter 3.1.8和Bootstrap 4中开发一个基本的博客应用程序 该应用程序允许注册和登录 使用md5函数加密的密码: $enc_password = md5($this->input->post('password')); 在登录控制器中,我有: 我有安全问题,所以我在寄存器控制器中将md5替换为密码\u散列: $enc_password = password_hash($this->input->post('password'), PAS

我正在Codeigniter 3.1.8和Bootstrap 4中开发一个基本的博客应用程序

该应用程序允许注册和登录

使用md5函数加密的密码:

$enc_password = md5($this->input->post('password'));
在登录控制器中,我有:

我有安全问题,所以我在寄存器控制器中将md5替换为密码\u散列:

$enc_password = password_hash($this->input->post('password'), PASSWORD_DEFAULT);
$hashed_password = password_hash($password, PASSWORD_DEFAULT);
注册工作正常,数据库中的密码字符串比以前更安全

我已将用户模型中的用户登录更新为:

public function user_login($email, $password) {
        $query = $this->db->get_where('authors', ['email' => $email, 'password' => $hashed_password]);
        return $query->row();
    }
其中$hash_密码来自登录控制器:

$enc_password = password_hash($this->input->post('password'), PASSWORD_DEFAULT);
$hashed_password = password_hash($password, PASSWORD_DEFAULT);
令我惊讶的是,这种密码匹配不起作用


我必须对登录代码进行的最小更改量是多少?

我已将用户提供的密码与密码散列进行了匹配,两个版本的代码之间的差异最小,方法是将用户登录修改为:

在登录控制器中,我有:


我希望这对我以外的许多人都有用。

如果使用密码散列,您不能直接检查字符串。因为它每次都给出不同的字符串

为了检查密码是否相等,您需要使用密码验证方法

你可以在这里拿到整个文件

链接如下:


我的朋友,OP在你回答之前8个小时用密码验证回答了他自己的问题。我没看到他回答了自己的问题。然而,我投了更高的票。谢谢@DFriend
public function user_login($email, $password) {
    $pass_hash_query = $this->db
        ->select('password')
        ->get_where('authors', ['email' => $email]);

$pass_hash = $pass_hash_query->row()->password;

    if (password_verify($password, $pass_hash)) {
        $query = $this->db->get_where('authors', ['email' => $email, 'password' => $pass_hash]);
        return $query->row();
    }
}
public function login() {  
    $this->form_validation->set_rules('email', 'Email', 'required|trim|valid_email');
    $this->form_validation->set_rules('password', 'Password', 'required|trim');
    $this->form_validation->set_error_delimiters('<p class="error-message">', '</p>');
    if ($this->form_validation->run()) {
      $email = $this->input->post('email');
      $password = $this->input->post('password');

      $this->load->model('Usermodel');
      $current_user = $this->Usermodel->user_login($email, $password);
        // If we find a user
      if ($current_user) {
        // If the user found is active
        if ($current_user->active == 1) {
          $this->session->set_userdata(
           array(
            'user_id' => $current_user->id,
            'user_email' => $current_user->email,
            'user_first_name' => $current_user->first_name,
            'user_is_admin' => $current_user->is_admin,
            'user_active' => $current_user->active,
            'is_logged_in' => TRUE
            )
           );
          // After login, display flash message
          $this->session->set_flashdata('user_signin', 'You have signed in');
          //and redirect to the posts page
          redirect('/');  
        } else {
          // If the user found is NOT active
          $this->session->set_flashdata("login_failure_activation", "Your account has not been activated yet.");
          redirect('login'); 
        }
      } else {
        // If we do NOT find a user
        $this->session->set_flashdata("login_failure_incorrect", "Incorrect email or password.");
        redirect('login'); 
      }
    }
    else {
      $this->index();
    }
}