codeigniter login with hash password PASSWORD_BCRYPT

Viewed 37

so I'm new to C.I and I have this kind of code from a tutorial for me it is clean. I manage to insert a hash password using "PASSWORD_BCRYPT" and it store to database successfully my problem is i can manage to full out the correct so that my login model will be successful.

MODEL FOR INSERT

public function insert_user(){

$password = $this->input->post('Password');
$hash = password_hash($password, PASSWORD_BCRYPT);

$data = array(
'Username' => $this->input->post('Username'),
'Password' => $hash,
'Position' => $this->input->post('Position'),
'Office' => $this->input->post('Office'),
); 
print_r($data);

return $this->db->insert('users', $data);

}

MODEL FOR LOGIN

public function login(){

$this->db->where('Username', $this->input->post('Username', true));
$this->db->where('Password', $this->input->post('Password', true));
$result = $this->db->get('users');

if($result->num_rows() == 1){

    return $result->row_array();
    
}else{
    return false;
}

controller

public function log_in(){

    $this->form_validation->set_error_delimiters('<div class="error">','</div>');
    $this->form_validation->set_rules('Username', 'Username', 'required');
    $this->form_validation->set_rules('Password', 'Password', 'required');

    if($this->form_validation->run() == FALSE){

        $page = "login";

        if(!file_exists(APPPATH.'views/pages/'.$page.'.php')){
            show_404();
        }
        
        $this->load->view('pages/'.$page);

        }else{

            $user_id = $this->Page_model->login();

            if($user_id){

                $user_data = array(
                    'Username' => $user_id['Username'],
                    'Position' => $user_id['Position'].' '.$user_id['Username'],
                    'Office' => $user_id['Office'],
                    'logged_in' => true

                );
            
                $this->session->set_userdata($user_data);
                $this->session->set_flashdata('user_log', 'You are now loged in as '
                .$this->session->Position);
                redirect(base_url());
            }else{
                $this->session->set_flashdata('failed', 'Username/Password not match');
                redirect(base_url().'log_in');

            }


            
           

        }
} 

is someone give me some tips how to make it working.

I try someway but as you expect no successful at all.

0 Answers
Related