首页 > 解决方案 > 如何使用散列密码添加密码验证系统?

问题描述

顺便说一句,我正在使用 PHP 和 CodeIgniter 版本 3,所以我目前正在开发一个新网站,用于记录任何市场的销售数据。我目前在注册系统中苦苦挣扎。我的意思是如何使用用户输入的密码验证散列密码。

这是脚本

public function cek_login()
    {
        $email          = set_value('email');
        $password       = set_value('password');
        

        $result         = $this->db->where('email', $email)
                                    ->where('password', $password)
                                    ->limit(1)
                                    ->get('account');
        if($result->num_rows() > 0){
            return $result->row();
        } else{
            return array();
        }
    }

` 和模型是

public function loginproses()
    {
        $this->form_validation->set_rules('email','email','required|valid_email');
        $this->form_validation->set_rules('password','password','required');
        if($this->form_validation->run() == FALSE)
        {
            redirect('/');
        } else{
            $auth = $this->M_login->cek_login();
            
            if($auth == FALSE)
            {
                
                echo '<script>
                window.location.href="'.base_url('C_login').'";
                alert("Terjadi kesalahan silahkan cek ulang Email dan Password anda");
                </script>';
            } else{
                    
                if($auth['verified'] = 1){
                     echo '<script>
                                window.location.href="'.base_url('C_login').'";
                                alert("Email");
                                </script>';
                }
                else{
                    $this->session->set_userdata('id', $auth->id);
                    $this->session->set_userdata('email', $auth->email);
                    $this->session->set_userdata('nama', $auth->nama);
                    echo '<script>
                    window.location.href="'.base_url('C_login').'";
                    alert("Login Berhasil");
                    </script>';
                }
            }
        }
    }

标签: phphtmlcodeigniter

解决方案


尽管我坚信您需要进行大量学习,但我会留下这个答案,以便在您进行项目时为您指明正确的方向。

对于注册,您需要使用 PHP 的内置哈希函数对密码进行哈希处理password_hash()。所以你的注册函数应该是这样的:

public function cek_register()
{
    // get other fields for the registration and validate
    $user_password = $this->input->post('password'); // password field

    // Hash the password
    $hashed_password = password_hash($user_password, PASSWORD_DEFAULT);

    // You can store the $hashed_password in your database as you wish
}

对于登录,您需要像这样验证密码:

public function cek_login()
{
    // First you check if the supplied email is stored in the database
    // If yes, you get the password field of that stored email
    // Then you verify the password if it matches which the user has entered like so:
    if ( password_verify($the_entered_password, $the_password_stored_in_the_database) )
    {
        // The user is valid, log user in
    }
    else 
    {
        // Wrong password, show error message
    }
}

以下是您应该查看的一些链接: 密码哈希 密码验证


推荐阅读