codeigniter中的哈希密码 [英] Hash password in codeigniter
本文介绍了codeigniter中的哈希密码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在codeigniter中哈希我的密码。我无法找到解决方法
这是我的插入模型。插入客户端详细信息时密码已加密。
我无法在登录时解密它
我尝试过:
< pre>公共函数insert_client($ codeDigits)
{
$ hash = $ this-> hash($ _ POST ['Password']);
$ response = $ this-> taken_email($ _ POST ['Email']);
if($ response){
$ return = false;
} else {
$ this-> FirstName = $ _POST ['FirstName']; //请阅读以下注释
$ this-> LastName = $ _POST ['LastName'];
$ this->电子邮件= $ _POST ['电子邮件'];
$ this-> Role_Id = 2;
$ this->密码= $ hash;
$ this-> PhoneNo = $ _POST ['PhoneNo'];
$ this-> confirmCode = $ codeDigits;
$ this-> db-> insert('users',$ this);
$ return = true;
}
返回$返回;
}
这是我的密码哈希模型
公共功能hash($ password)
{
$ hash = password_hash($ password,PASSWORD_DEFAULT);
返回$ hash;
}
//验证密码
公共函数verifyHash($ password,$ vpassword)
{
if(password_verify($ password,$ vpassword) )
{
返回TRUE;
}
else {
返回FALSE;
}
}
登录时我无法解密密码,无论如何,密码在数据库中正确加密。
公共功能login_model($ email,$ password)
{
$ this - > DB->选择( '*');
$ this-> db-> from('users');
$ this-> db-> where('Email',$ email);
$ this-> db-> where('Password',$ password);
$ this-> db-> where('Role_Id!=',1);
$ query = $ this-> db-> get();
if($ query-> num_rows()> 0)
{
$ results = $ query-> row();
//将结果存储在变量$ data
foreach($ results as $ data)
{
if($ this-> verifyHash($ this) - > $ password,$ data-> password == TRUE))
{
$ dat = array(
'id_user'=> $ data-> id_User,
'FirstName'=> $ data-> FirstName,
'LastName'=> $ data-> LastName,
'Pononumber'=> $ data-> PhoneNo,
'电子邮件'=> $ data->电子邮件,
'角色'=> $ data-> Role_Id,
'imageUrl'=> $ data-> imageUrl,
'category_id'=> $ data-> category_id,
'IdType'=> $ data-> IdType,
'IdNumber'=> $ data-> IdNumber ,
'DOB'=> $ data-> DOB,
'confirmCode => $ DATA-> confirmCode,
确认 => $ data->已确认,
'积分'=> $ data->积分
);
}
$ this-> session-> set_userdata($ dat);
返回true;
}
}
else {
返回false;
}
}
这是我在控制器中的登录功能
公共职能post_login()
{
$ this-> form_validation-> set_rules('Email','Email','trim | required | MIN_LENGTH [6]');
$ this-> form_validation-> set_rules('密码','密码','trim | required | min_length [6]');
if($ this-> form_validation-> run()== TRUE){
if($ this-> Users_model-> login_model($ _ POST ['Email' ],$ _ POST ['密码'])){
//测试重定向
if($ _ SESSION ['confirmed'] == true){
if($ _SESSION ['role) '] == 2){
redirect(客户/欢迎);
}否则if($ _SESSION ['role'] == 3){
redirect(Pro / welcome);
}
} else {
redirect(Welcome / confirmCode);
}
//测试重定向
}其他{
//
$ this-> session-> set_flashdata('err',真正);
redirect(欢迎/登录);
}
}其他{
$ this-> login();
}
}
如何在登录时解密密码。
解决方案
< blockquote> codeDigits)
{
hash =
this-> hash(
I want to hash my passwords in codeigniter.I can't find a way around it
this is my insert model.the password is encrypted when inserting the client details.
I cant decrypt it when logging in
What I have tried:
<pre>public function insert_client($codeDigits) { $hash = $this->hash($_POST['Password']); $response = $this->taken_email($_POST['Email']); if($response){ $returned = false; }else{ $this->FirstName = $_POST['FirstName']; // please read the below note $this->LastName = $_POST['LastName']; $this->Email = $_POST['Email']; $this->Role_Id = 2; $this->Password = $hash; $this->PhoneNo = $_POST['PhoneNo']; $this->confirmCode = $codeDigits; $this->db->insert('users', $this); $returned = true; } return $returned; }
this is my password hash model
public function hash($password) { $hash = password_hash($password,PASSWORD_DEFAULT); return $hash; } //verify password public function verifyHash($password,$vpassword) { if(password_verify($password,$vpassword)) { return TRUE; } else{ return FALSE; } }
during login i can't decrypt the password,the password get encrypted in the database correctly anyway.
public function login_model($email,$password) { $this->db->select('*'); $this->db->from('users'); $this->db->where(' Email',$email); $this->db->where('Password',$password); $this->db->where('Role_Id !=',1); $query = $this->db->get(); if($query->num_rows() > 0) { $results = $query->row(); // storing the results in the variable $data foreach($results as $data) { if($this->verifyHash($this->$password,$data->password == TRUE)) { $dat = array( 'id_user' => $data->id_User, 'FirstName' => $data->FirstName, 'LastName' => $data->LastName, 'Phonenumber' => $data->PhoneNo, 'Email' => $data->Email, 'role' => $data->Role_Id, 'imageUrl' => $data->imageUrl, 'category_id' => $data->category_id, 'IdType' => $data->IdType, 'IdNumber' => $data->IdNumber, 'DOB' => $data->DOB, 'confirmCode' => $data->confirmCode, 'confirmed' => $data->confirmed, 'Points'=> $data->Points ); } $this->session->set_userdata($dat); return true; } } else{ return false; } }
this is my login function in the controller
public function post_login() { $this->form_validation->set_rules('Email', 'Email', 'trim|required|min_length[6]'); $this->form_validation->set_rules('Password', 'Password', 'trim|required|min_length[6]'); if($this->form_validation->run() == TRUE ){ if($this->Users_model->login_model($_POST['Email'],$_POST['Password'])){ //test for redirect if($_SESSION['confirmed'] == true) { if ($_SESSION['role'] == 2) { redirect("Client/welcome"); } else if ($_SESSION['role'] == 3) { redirect("Pro/welcome"); } }else{ redirect("Welcome/confirmCode"); } // test for redirect }else{ // $this->session->set_flashdata('err', true); redirect("Welcome/login"); } }else{ $this->login(); } }
How can I decrypt the password when logging in.
解决方案
codeDigits) {
hash =
this->hash(
这篇关于codeigniter中的哈希密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文