here is my code, i am trying to have an admin access through the database... but it seems like my usertype validation doesnot get anything. i tried to print_r it and the output is only array(), what do you think is my error. here is my code..
login controller
public function loginValidate(){
$this->load->library('form_validation');
$this->load->model('login_model');
$this->form_validation->set_rules('USERNAME','Username','required|trim|callback_validateCreds');
$this->form_validation->set_rules('PASSWORD','Password','required|trim|md5');
if ($this->form_validation->run() == FALSE){
$this->index();
}else{
$data = array(
'USERNAME' => $this->input->post('USERNAME'),
'is_logged_in' => 1,
'value' => $this->login_model->returnQuery()
);
$this->session->set_userdata($data);
redirect('login/adminIndex');
$q = $this->login_model->returnQuery();
if ($q = '1'){
redirect('login/adminIndex');
}elseif ($q = '2') {
redirect('login/userIndex');
}else{
redirect('login/restricted');
}
}
}
public function validateCreds(){
$this->load->library('form_validation');
$this->load->model('login_model');
if($this->login_model->can_log_in()){ // can_log_in() model
return true;
}else{
$this->form_validation->set_message('validateCreds','Username and password did not match.'); //return message to validateCreds
return false;
}
}
here is my models
public function can_log_in(){
$sql ="SELECT * FROM tbl_users WHERE USERNAME = ? AND PASSWORD = ?";
$data = array(
'USERNAME' => $this->input->post('USERNAME'),
'PASSWORD' => $this->input->post('PASSWORD')
);
$query = $this->db->query($sql, $data);
return $query;
if ($query->num_rows() == 1){
return true;
}else{
return false;
}
}
public function returnQuery(){
$sql = "SELECT USERCODE FROM tbl_users where USERNAME =? AND PASSWORD = ?";
$data = array(
'USERNAME' => $this->input->post('USERNAME'),
'PASSWORD' => $this->input->post('PASSWORD')
);
$query = $this->db->query($sql, $data);
return $query->result();
}
does not have any errors but returns true to
if ($q = '1'){
redirect('login/adminIndex');
even if its not = 1.. i also tried == and taking off '' but it only directs me to the restricted page.
First of all keep in mind you are returning the complete array not a single value so change model function as:
public function returnQuery(){
$sql = "SELECT USERCODE FROM tbl_users where USERNAME =? AND PASSWORD = ?";
$data = array( 'USERNAME' => $this->input->post('USERNAME'), 'PASSWORD' => $this->input->post('PASSWORD') );
$query = $this->db->query($sql, $data);
$result = $query->result_array();
if(count($result) > 0){
return $result[0]["USERCODE"];
}
else{
return 0;
}
}
And than compare value not assign the value:
if($q == 1){
//your redirection stuff
}
elseif($q == 2){
//your redirection stuff
}
else{
//your redirection stuff
}
UPDATE 1:
You have also issue in callback function. You are using return $query
before checking the number of records.
Modified function:
public function can_log_in(){
$sql ="SELECT * FROM tbl_users WHERE USERNAME = ? AND PASSWORD = ?";
$data = array( 'USERNAME' => $this->input->post('USERNAME'), 'PASSWORD' => $this->input->post('PASSWORD') );
$query = $this->db->query($sql, $data);
//return $query;
if ($query->num_rows() == 1)
{
return true;
}
else{
return false;
}
}