Php 如何使用CodeIgniter的密码\u哈希
我正在研究CodeIgniter框架,并试图构建一个登录和注册系统。底部是控制器中用于登录和注册的两个函数 人们建议我使用'password'=>password\u hash$this->input->post'password',password\u BCRYPT$选项,而不是MD5作为密码。我试着这么做,但没有成功 你们能帮我吗?我是否需要更改模型或视图中的其他内容?非常感谢你们 控制器:登录功能Php 如何使用CodeIgniter的密码\u哈希,php,codeigniter,codeigniter-3,Php,Codeigniter,Codeigniter 3,我正在研究CodeIgniter框架,并试图构建一个登录和注册系统。底部是控制器中用于登录和注册的两个函数 人们建议我使用'password'=>password\u hash$this->input->post'password',password\u BCRYPT$选项,而不是MD5作为密码。我试着这么做,但没有成功 你们能帮我吗?我是否需要更改模型或视图中的其他内容?非常感谢你们 控制器:登录功能 if($this->input->post('loginSubmit')){
if($this->input->post('loginSubmit')){
$this->form_validation->set_rules('email', 'Email', 'required|valid_email');
$this->form_validation->set_rules('password', 'password', 'required');
if ($this->form_validation->run() == true) {
$con['returnType'] = 'single';
$con['conditions'] = array(
'email'=>$this->input->post('email'),
'password' => md5($this->input->post('password')),
'status' => '1'
);
$checkLogin = $this->user->getRows($con);
if($checkLogin){
$this->session->set_userdata('isUserLoggedIn',TRUE);
$this->session->set_userdata('userId',$checkLogin['id']);
redirect('users/account');
}else{
$data['error_msg'] = 'Wrong email or password, please try again.';
}
}
}
$data = array();
$userData = array();
if($this->input->post('regisSubmit')){
$this->form_validation->set_rules('name', 'Name', 'required');
$this->form_validation->set_rules('email', 'Email', 'required|valid_email|callback_email_check');
$this->form_validation->set_rules('password', 'password', 'required');
$this->form_validation->set_rules('conf_password', 'confirm password', 'required|matches[password]');
$userData = array(
'name' => strip_tags($this->input->post('name')),
'email' => strip_tags($this->input->post('email')),
'password' => md5($this->input->post('password')),
'gender' => $this->input->post('gender'),
'phone' => strip_tags($this->input->post('phone'))
);
if($this->form_validation->run() == true){
$insert = $this->user->insert($userData);
if($insert){
$this->session->set_userdata('email',$userData['email']);
redirect('email');
}else{
$data['error_msg'] = 'Some problems occured, please try again.';
}
注册函数
if($this->input->post('loginSubmit')){
$this->form_validation->set_rules('email', 'Email', 'required|valid_email');
$this->form_validation->set_rules('password', 'password', 'required');
if ($this->form_validation->run() == true) {
$con['returnType'] = 'single';
$con['conditions'] = array(
'email'=>$this->input->post('email'),
'password' => md5($this->input->post('password')),
'status' => '1'
);
$checkLogin = $this->user->getRows($con);
if($checkLogin){
$this->session->set_userdata('isUserLoggedIn',TRUE);
$this->session->set_userdata('userId',$checkLogin['id']);
redirect('users/account');
}else{
$data['error_msg'] = 'Wrong email or password, please try again.';
}
}
}
$data = array();
$userData = array();
if($this->input->post('regisSubmit')){
$this->form_validation->set_rules('name', 'Name', 'required');
$this->form_validation->set_rules('email', 'Email', 'required|valid_email|callback_email_check');
$this->form_validation->set_rules('password', 'password', 'required');
$this->form_validation->set_rules('conf_password', 'confirm password', 'required|matches[password]');
$userData = array(
'name' => strip_tags($this->input->post('name')),
'email' => strip_tags($this->input->post('email')),
'password' => md5($this->input->post('password')),
'gender' => $this->input->post('gender'),
'phone' => strip_tags($this->input->post('phone'))
);
if($this->form_validation->run() == true){
$insert = $this->user->insert($userData);
if($insert){
$this->session->set_userdata('email',$userData['email']);
redirect('email');
}else{
$data['error_msg'] = 'Some problems occured, please try again.';
}
这是我在模型中的getRows函数
function getRows($params = array()){
$this->db->select('*');
$this->db->from($this->userTbl);
//fetch data by conditions
if(array_key_exists("conditions",$params)){
foreach ($params['conditions'] as $key => $value) {
$this->db->where($key,$value);
}
}
if(array_key_exists("id",$params)){
$this->db->where('id',$params['id']);
$query = $this->db->get();
$result = $query->row_array();
}else{
//set start and limit
if(array_key_exists("start",$params) && array_key_exists("limit",$params)){
$this->db->limit($params['limit'],$params['start']);
}elseif(!array_key_exists("start",$params) && array_key_exists("limit",$params)){
$this->db->limit($params['limit']);
}
$query = $this->db->get();
if(array_key_exists("returnType",$params) && $params['returnType'] == 'count'){
$result = $query->num_rows();
}elseif(array_key_exists("returnType",$params) && $params['returnType'] == 'single'){
$result = ($query->num_rows() > 0)?$query->row_array():FALSE;
}else{
$result = ($query->num_rows() > 0)?$query->result_array():FALSE;
}
}
//return fetched data
return $result;
}
您可以通过使用CodeIgniter中的密码\u散列函数来实现
现在,您可以在同一控制器内的任何位置调用此命令,如下所示:
$userData = array(
'name' => strip_tags($this->input->post('name')),
'email' => strip_tags($this->input->post('email')),
'password' =>$this->custom_password_hash($this->input->post('password')),
'gender' => $this->input->post('gender'),
'phone' => strip_tags($this->input->post('phone'))
);
希望这对您有意义。您可以通过使用CodeIgniter中的密码散列函数来实现
现在,您可以在同一控制器内的任何位置调用此命令,如下所示:
$userData = array(
'name' => strip_tags($this->input->post('name')),
'email' => strip_tags($this->input->post('email')),
'password' =>$this->custom_password_hash($this->input->post('password')),
'gender' => $this->input->post('gender'),
'phone' => strip_tags($this->input->post('phone'))
);
希望这对您有意义。希望这能帮助您: 在注册中:在使用:password\u hash这样注册时哈希密码 登录时:使用密码检查密码\u登录时验证 模型的方法getRows应如下所示:
public function getRows($where = array())
{
if (! empty($where))
{
$this->db->where($where);
$query = $this->db->get('users');
if ($query->num_rows() > 0)
{
return $query->row_array();
}
}
else
{
$query = $this->db->get('users');
if ($query->num_rows() > 0)
{
return $query->result_array();
}
}
}
更多信息:希望这能帮助您: 在注册中:在使用:password\u hash这样注册时哈希密码 登录时:使用密码检查密码\u登录时验证 模型的方法getRows应如下所示:
public function getRows($where = array())
{
if (! empty($where))
{
$this->db->where($where);
$query = $this->db->get('users');
if ($query->num_rows() > 0)
{
return $query->row_array();
}
}
else
{
$query = $this->db->get('users');
if ($query->num_rows() > 0)
{
return $query->result_array();
}
}
}
更多信息:md5到密码\u散列以实现更好的密码加密
在寄存器控制器中:
更改:
致:
在登录控制器中-删除密码作为$this->user->getRows.的条件。。并随后将密码\u验证添加到登录检查中
确保$checkLogin['password']也从$this->user->getRows$con返回,用于验证密码。。工作
更新代码:
md5到密码\u散列以实现更好的密码加密
在寄存器控制器中:
更改:
致:
在登录控制器中-删除密码作为$this->user->getRows.的条件。。并随后将密码\u验证添加到登录检查中
确保$checkLogin['password']也从$this->user->getRows$con返回,用于验证密码。。工作
更新代码:
嗨,谢谢你的帮助!现在注册工作真的很好,但我一直失败,当我登录我认为可能是模型的问题?。我上传了模型方法中的getRows函数。你能看一下并告诉我哪里错了吗?非常感谢亲爱的,为什么不让它保持简单呢?看看我的答案中的getRows方法,如果它返回row_数组,那么使用一个名为getRows的模型方法是没有意义的——在这种情况下,将其称为getRow会更有意义。如果存在where条件,为什么要限制返回的行数?如果需要where条件和多行,该怎么办?您好,谢谢您的帮助!现在注册工作真的很好,但我一直失败,当我登录我认为可能是模型的问题?。我上传了模型方法中的getRows函数。你能看一下并告诉我哪里错了吗?非常感谢亲爱的,为什么不让它保持简单呢?看看我的答案中的getRows方法,如果它返回row_数组,那么使用一个名为getRows的模型方法是没有意义的——在这种情况下,将其称为getRow会更有意义。如果存在where条件,为什么要限制返回的行数?如果存在where条件并且需要多行,该怎么办?您需要仔细阅读。$Options部分用于存储成本,你需要仔细阅读这篇文章。$Options部分用于存储成本,标签可能会损坏有效的电子邮件地址。如果愿意,可以验证此值,但我不建议对其进行消毒。我想我也不会对这个名字或电话进行消毒。不管怎样,每次将字符串打印到html文档中时,您都将使用html编码该字符串。电话号码一开始可能会有非常严格的验证——哪些标签永远不会存在。strip_标签可能会损坏有效的电子邮件地址。如果愿意,可以验证此值,但我不建议对其进行消毒。我想我也不会对这个名字或电话进行消毒。不管怎样,每次将字符串打印到html文档中时,您都将使用html编码该字符串。电话号码一开始可能会有非常严格的验证——哪些标签永远不会存在。
'password' => password_hash($this->input->post('password'),PASSWORD_DEFAULT),
if($this->input->post('loginSubmit')){
$this->form_validation->set_rules('email', 'Email', 'required|valid_email');
$this->form_validation->set_rules('password', 'password', 'required');
if ($this->form_validation->run() == true) {
$con['returnType'] = 'single';
$con['conditions'] = array(
'email'=>$this->input->post('email'),
'status' => '1'
);
$checkLogin = $this->user->getRows($con); // assumes 'password' field is also returned..
if(password_verify($this->input->post('password'), $checkLogin['password']){
$this->session->set_userdata('isUserLoggedIn',TRUE);
$this->session->set_userdata('userId',$checkLogin['id']);
redirect('users/account');
}else{
$data['error_msg'] = 'Wrong email or password, please try again.';
}
}
}