【问题标题】:password_verify() not working properlypassword_verify() 无法正常工作
【发布时间】:2023-03-11 03:14:01
【问题描述】:

我的控制器

// display the login page
    public function index() {
        // on form data
        $onsumbit = $this->input->post('verify');
        if(isset($onsumbit)) {

           $user_name = $this->input->post('user_name');
           $password = $this->input->post('password');

            // verify login
            $verified = $this->login_model->login_verify($user_name,$password);
            // success
            if($verified) {
                redirect('dashboard');
            }
            // failure
            else {
                $this->session->set_flashdata('login_failure','Please check your email and password and try again');
                redirect('index');
            }
        }
        // login page
        $this->load->view('login');
    }

我的模型

public function login_verify($user_name,$password) {
        $hashed_password = $this->verify_password($user_name);
        $this->db->where('user_name',$user_name)->where('password',password_verify($password, $hashed_password));
        $result = $this->db->get('employee');
        if($result -> num_rows() > 0) {

        $session = array(
            'employee_id'   => $result->row()->employee_id,
            'name'          => $result->row()->first_name.' '.$result->row()->last_name,
            'employee_role' => $result->row()->employee_role,
            'is_logged_in'  => TRUE,
        );
        // set session
        $this->session->set_userdata($session);
        return TRUE;
        } else {
            return FALSE;
        }

    }

     private function verify_password($user_name) {
        $this->db->where('user_name',$user_name);
        $result = $this->db->get('employee');
        if($result -> num_rows() > 0) {
         return  $get_password = $result->row(0)->password;
        }

    }

我正在对我的登录名进行密码散列,我添加了默认密码散列()。 当我验证密码无法正常工作时,任何密码类型都会登录到仪表板。我在这里忘记了什么,任何帮助将不胜感激。

【问题讨论】:

  • 你不能这样做。 password_verify 适用于实际密码哈希,您无法使用password_verify 从数据库中进行选择。使用两个步骤: 1. 获取用户和密码哈希 2. 验证密码哈希。 password_hash 每次调用它时都会给你一个不同的盐(因此也会给你一个不同的哈希),所以不像像md5 这样的坏哈希,你不能直接在数据库中比较哈希。
  • 谢谢,它就像黄油牛奶一样

标签: php codeigniter php-password-hash


【解决方案1】:

你可以稍微简化一下,但是正如@h2ooooooo 提到的,你不能从数据库中选择password_verify

这是我用于身份验证的内容:

public function login()
{
    $this->form_validation->set_rules('email', 'E-mail', 'required|valid_email');
    $this->form_validation->set_rules('password', 'Password', 'required');

    if ($this->form_validation->run()) {

        // Get the actual user from the database, you can use email or username, whatever you want
        $user = $this->user->get($this->input->post('email'));

        // If we have a user, then we can check against the submitted password:
        if ($user && password_verify($this->input->post('password'), $user->password)) {
            $this->session->set_userdata([
                // Your session data 
            ]);
            redirect('/');
        } else {
            $this->session->set_flashdata('error', 'Wrong credintals');
            redirect('login');
        }
    }

    $this->load->view('auth/login');
}

【讨论】:

  • 这很酷,但我在同一个功能中做了,现在效果很好
猜你喜欢
  • 2020-02-18
  • 2016-12-01
  • 2018-03-02
  • 1970-01-01
  • 2016-09-01
  • 2017-10-22
  • 2012-07-11
  • 2018-04-08
  • 2017-04-20
相关资源
最近更新 更多