In my codeigniter login I am not having any no luck logging on. I throws One error now called. Undefined property: error message
. I also am not sure if my validate is correct if that is why not picking up user.
Error
Message: Undefined property: CI_DB_mysqli_driver::$num_rows
Library File
public function login() {
$this->CI =& get_instance();
$this->CI->load->library('session');
$this->CI->load->library('security/encryption');
$password = $this->CI->input->post('password');
$username = $this->CI->input->post('username');
$user_query = $this->CI->db->select('*')
->where('username', $username)
->where('password', $this->CI->encryption->hash_password().hash('sha512', $password).$this->CI->encryption->generate_salt())
->where('status', "1")
->from('user');
if($user_query->num_rows) {
$data = array(
'ip' => $this->CI->input->ip_address(),
'user_id' => $this->CI->session->userdata('user_id')
);
$this->db->where('user_id', $user_id);
$this->db->update('user', $data);
return true;
} else {
return false;
}
}
I use my own form validation method rather than codeigniter way. My way I have more control over it.
Controller
public function index() {
$this->load->library('users');
$this->lang->load('common/login', 'english')
if(($this->input->server('REQUEST_METHOD') == 'POST') && $this->validate()) {
$data = array(
'username' => $this->input->post(),
'user_id' => $row->user_id,
'isLogged' => true
);
$this->db->get('user');
$this->session->set_userdata($data);
redirect('dashboard');
}
if (array_key_exists('warning', $this->error)) {
$data['error_warning'] = $this->error['warning'];
} else {
$data['error_warning'] = '';
}
if (array_key_exists('username', $this->error)) {
$data['error_username'] = $this->error['username'];
} else {
$data['error_username'] = '';
}
if (array_key_exists('password', $this->error)) {
$data['error_password'] = $this->error['password'];
} else {
$data['error_password'] = '';
}
$data['action'] = site_url("login");
if (!"" == trim($this->input->post('username'))) {
$data['username'] = $this->input->post('username');
} else {
$data['username'] = '';
}
if (!"" == trim($this->input->post('password'))) {
$data['password'] = $this->input->post('password');
} else {
$data['password'] = '';
}
$this->load->view('template/common/login', $data);
}
protected function validate() {
if (!"" == trim($this->input->post('username')) || !"" == trim($this->input->post('password')) || !$this->users->login($this->input->post('username'), $this->input->post('password'))) {
$this->error['warning'] = $this->lang->line('error_login');
}
return !$this->error;
}
}