Lberteh Lberteh - 6 months ago 28
PHP Question

Codeigniter Trying to get property of non object

I have developed a web application using Codeigniter. It all works perfectly on localHost, but now that I uploaded it to a server I have one single issue. Everything works as it's supposed to except my redirect when the user inserts the wrong password. It was supposed to redirect back to login page and display an error message (which works with localhost ), but I get the following errors.

image showing errors

I decided to not redirect and simply load my login view again, it worked, but I still get the Trying to get property of non object error message, which is weird cause I didn't get this error working on localhost.

My controller:

<?php

defined('BASEPATH') OR exit('No direct script access allowed');

class Login extends CI_Controller {

public function index()
{
$this->load->view('header');
$this->load->view('login_view');
$this->load->view('footer');
}

public function to_login() {

$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
$this->form_validation->set_rules('password', 'Password', 'trim|required');

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

$data = array(

'errors' => validation_errors()

);

$this->session->set_flashdata($data);

redirect('index.php/Login');

} else {

$email = $this->input->post('email');
$password = $this->input->post('password');

// the following lines are 38 and 39
$user_id = $this->Login_model->login_user($email, $password)->user_id;
$first_name = $this->Login_model->login_user($email, $password)->first_name;

if($user_id) {

$user_data = array(

'user_id' => $user_id,
'first_name' => $first_name,
'logged_in' => true

);

$this->session->set_userdata($user_data);

redirect('http://gc200322197.computerstudi.es/app/index.php/Main');

} else {

$this->session->set_flashdata('login_fail', 'Invalid login. Please check the email and password fields.');
$this->load->view('header');
$this->load->view('login_view');
$this->load->view('footer');


}

}

}


My model:

<?php

class Login_model extends CI_Model{

public function login_user($email, $password) {

$this->db->where('email', $email);

$result = $this->db->get('users');

$db_password = $result->row(4)->password;

if(password_verify($password, $db_password)) {

return $result->row(0);

} else {

return false;

}

}

}


I've read so many questions about this error and I still couldn't figure out what is wrong and why it works on localhost and not online. I'd really appreciate some help on this.

Answer

Focusing on line #38, your model is returning one of two datatypes and your controller code is not accommodating it:

$user_id = $this->Login_model->login_user($email, $password)->user_id;

should be changed into:

$user_id = $this->Login_model->login_user($email, $password); // This could be false or an object

// Check if we've received an object
if($user_id){
    $user_id = $user_id->user_id; // Access the object's property
    $first_name = $user_id->first_name; // Might as well set the first name as well
}

A better option would be renaming some variables like this:

$user_info = $this->Login_model->login_user($email, $password); // This could be false or an object

// Check if we've received an object
if($user_info){
    $user_id = $user_info->user_id; // Access the object's property
    $first_name = $user_info->first_name; // Might as well set the first name as well
}

Update:

For anyone confused about CodeIgniter models, speaking to v3.0.4, see below:

Example 1

$this->load->model('Login_model');

$this->Login_model->some_func(); // Works
$this->login_model->some_func(); // Fails

Example 2

$this->load->model('login_model');

$this->Login_model->some_func(); // Fails
$this->login_model->some_func(); // Works

It is only a recommendation that you should declare and access models with lowercase, example #2; it will make your life easier.

Based on OPs code and comments, it appears that they have loaded the model using example #1 so merely suggesting to use $this->login_model would break the code further.