php - 如何使用散列密码添加密码验证系统?
问题描述
顺便说一句,我正在使用 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 的内置哈希函数对密码进行哈希处理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
}
}
推荐阅读
- javascript - ngx-spinner 微调器隐藏 Angular 中的所有其他微调器?
- javascript - 用数据库中的文本替换占位符
- javascript - 使用 useState 和 useEffect 时出现“TypeError:无法读取未定义的属性‘名称’”
- python - 为什么我不能用 django 加载图像?
- jenkins - 在 Kubernetes 中挂载持久“动态”文件的解决方案
- mysql - 对于这种复杂的场景,是否可以在 SQL 中使用递归 CTE 来做到这一点?请建议我任何替代方式
- python - 通过 python 和 EWS 为 Exchange 帐户添加新委托
- extjs - ExtJS 6 tabindex 坏了
- react-native - AWS Cognito Pre Sign Up 触发器未针对托管 UI 运行 Google Auth 需要添加出生日期属性
- javascript - Mapbox GL JS - 将弹出标记与地理定位相结合