Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

SOLVED: How to use password_hash for CodeIgniter

seamas zhou:

I am studying the CodeIgniter framework and trying to build a login and registration system. In the bottom are two functions in the controllers for login and registration. People suggest me using "'password' => password_hash($this->input->post('password'), PASSWORD_BCRYPT, $options)," instead of MD5 for password. I tried to do that but not works. Can you guys help me with this? Do I need change anything else in my model or view? Thank you guys very much

Controllers: Login function


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.';
}
}
}

registration function


$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.';
}

And here is my getRows function in the models.


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;
}



Posted in S.E.F
via StackOverflow & StackExchange Atomic Web Robots


This post first appeared on Stack Solved, please read the originial post: here

Share the post

SOLVED: How to use password_hash for CodeIgniter

×

Subscribe to Stack Solved

Get updates delivered right to your inbox!

Thank you for your subscription

×