【问题标题】:How to add password verify system with the hashed password? [closed]如何使用散列密码添加密码验证系统? [关闭]
【发布时间】:2020-12-03 15:57:29
【问题描述】:

顺便说一下,我正在使用 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>';
                }
            }
        }
    }

【问题讨论】:

标签: php html codeigniter


【解决方案1】:

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

注册时,您需要使用 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
    }
}

以下是您应该查看的一些链接: Password Hashing Password Verify

【讨论】:

    猜你喜欢
    • 2017-12-26
    • 1970-01-01
    • 2019-03-19
    • 2020-02-29
    • 2020-08-16
    • 1970-01-01
    • 2019-05-24
    • 2016-12-11
    • 2016-05-22
    相关资源
    最近更新 更多