I am new to codeigniter,and I am using 3.1.5 version. Below is my user login function.
class Auth extends CI_Controller
{
public function login()
{
// Load Login View
$this->load->view('login');
// validate form
$this->form_validation->set_rules('username','Username','required');
$this->form_validation->set_rules('password','Password','required|min_length[5]');
if($this->form_validation->run()== true){
$username = $_POST['username'];
$password = $_POST['password'];
// check user in database
$this->db->select("*");
$this->db->from("users");
$this->db->where(array("username"=>$username,"password"=>$password));
$query = $this->db->get();
$user_data = $query->row();
// check if user exists or not
if($user_data->username){
// show temporary success message
$this->session->set_flashdata("success","User successfully logged in");
// set temporary sesstion variables
$_SESSION['user_logged'] = TRUE;
$_SESSION['username'] = $user_data->username;
}else{
$this->session->set_flashdata("error","Invalid username, user not exist");
}
}
}
When i provide user credentials like username and password I am getting the below error and not able to solve this.
Severity: Notice
Message: Trying to get property of non-object
Filename: controllers/Auth.php
Line Number: 29
Backtrace:
File: D:\xampp\htdocs\ci315\application\controllers\Auth.php
Line: 29
Function: _error_handler
File: D:\xampp\htdocs\ci315\index.php
Line: 315
Function: require_once
The line number 29 exactly from here
if($user_data->username){
// show temporary success message
$this->session->set_flashdata("success","User successfully logged in");
// set temporary sesstion variables
$_SESSION['user_logged'] = TRUE;
$_SESSION['username'] = $user_data->username;
}else{
$this->session->set_flashdata("error","Invalid username, user not exist");
}
If i comment the above section then there is no error. Please suggest me where I am doing wrong. Any help would be greatly appreciated, Thanks in advance.
Used below code this working fine. used if($query->num_rows() > 0) instead of if($user_data->username) and $query->row()->username instead of $user_data->username
class Auth extends CI_Controller
{
public function login()
{
// Load Login View
$this->load->view('login');
// validate form
$this->form_validation->set_rules('username','Username','required');
$this->form_validation->set_rules('password','Password','required|min_length[5]');
if($this->form_validation->run()== true){
$username = $_POST['username'];
$password = $_POST['password'];
// check user in database
$this->db->select("*");
$this->db->from("users");
$this->db->where(array("username"=>$username,"password"=>$password));
$query = $this->db->get();
if($query->num_rows() > 0){
// show temporary success message
$this->session->set_flashdata("success","User successfully logged in");
// set temporary sesstion variables
$_SESSION['user_logged'] = TRUE;
$_SESSION['username'] = $query->row()->username;
}else{
$this->session->set_flashdata("error","Invalid username, user not exist");
}
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.