Hi all I have this issue when I validate log in in codeigniter that seems it does not check the required password in my database.The required password in my database is hash using this
$password_hash = password_hash($password, PASSWORD_BCRYPT);
I'm also using this hash to test of it's ability and security also.
the code in my log in view is:
<div class="container">
<div class="card card-login mx-auto mt-5">
<div class="card-header">Login</div>
<div class="card-body">
<form method = "post" action=<?php echo base_url("Ec_controller/login"); ?> >
<div class="form-group">
<label for="Username">Username</label>
<input class="form-control" id="username" name="username" type="text" aria-describedby="emailHelp" placeholder="Enter Username">
</div>
<div class="form-group">
<label for="Password">Password</label>
<input class="form-control" id="password" name= "password" type="password" placeholder="Enter Password">
</div>
<div class="form-group">
<div class="form-check">
<label class="form-check-label">
<!-- <input class="form-check-input" type="checkbox"> Remember Password</label> -->
</div>
</div>
<input type="submit" name="submit" id="submit" class="btn btn-primary btn-xm" value="Log In" />
</form>
<div class="text-center">
<!-- <a class="d-block small" href="#">Forgot Password?</a> -->
</div>
</div>
</div>
</div>
On my controller:
public function login(){
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'Username', 'required|trim|callback_validate_credentials');
$this->form_validation->set_rules('password', 'Password', 'required|trim');
$username = $this->input->post('username');
$password = $this->input->post('password');
$user_id ="";
if($this->form_validation->run()){
$data = array(
'log_username' => $username,
'is_logged_in' =>1
);
$this->session->set_userdata($data);
$sql2 = $this->db->select("log_username, log_password,log_userlevel ")
->from("ec_login")
->where("log_username", $username)
->get();
foreach($sql2->result() as $user_level){
$user_id = $user_level->log_userlevel;
}
if($user_id == 1){
redirect('Ec_controller/view_admin');
}elseif ($user_id == 2) {
redirect('Ec_controller/view_it');
}else{
redirect("Ec_controller/index");
}
}else{
redirect('Ec_controller/index');
}
}
public function validate_credentials(){
$this->load->model('Ec_model');
if($this->Ec_model->can_log_in()){
return true;
}else{
$this->form_validation->set_message('validate_credentials', '<font color=red>Incorrect username/password</font>');
return false;
}
}
and on my Model:
public function can_log_in(){
$this->db->where('log_username', $this->input->post('username'));
$this->db->where('log_password', password_verify($this->input->post('password'), PASSWORD_BCRYPT));
$query = $this->db->get('ec_login');
if($query->num_rows() == 1)
{
return true;
}else{
return false;
}
}
When I put username it validates the required username and the only problem is the password that whatever i put on the password it validated and redirect to specific page/views, it sounds crazy. A help and a little explanation would great help.