1

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.

Sparky
  • 98,165
  • 25
  • 199
  • 285
Lberteh
  • 165
  • 13
  • So, which ones are line #38 and #39? It appears that your controller is completely missing the class constructor such as `class Login extends CI_controller{` – MonkeyZeus May 24 '16 at 18:20
  • Oh, I'm sorry, I did not paste the whole controller code, only the function, the class is there, exactly like in your comment. Lines 38 and 39 are the following: $user_id = $this->Login_model->login_user($email, $password)->user_id; $first_name = $this->Login_model->login_user($email, $password)->first_name; – Lberteh May 24 '16 at 18:27
  • 1
    Is the `Login_model` loaded? Usually model names are declared and accessed via lowercase like this `$this->login_model->login_user();` – MonkeyZeus May 24 '16 at 18:31
  • It is. The whole app works except for when you insert the wrong email or password. It does get the user id and first_name, cause when I login with the correct data it takes me to my home page and if I send data to my database with a form there is there, it sends the data correctly, including the user_id and it also displays the logged user first name. – Lberteh May 24 '16 at 18:34
  • I see, I've posted an answer. – MonkeyZeus May 24 '16 at 18:38
  • @MonkeyZeus comment is correct. The model name should be all lower case when accessed. See: http://www.codeigniter.com/user_guide/general/models.html#anatomy-of-a-model – Sparky May 24 '16 at 18:47

1 Answers1

1

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.

MonkeyZeus
  • 20,375
  • 4
  • 36
  • 77
  • As per CodeIgniter docs, the model is accessed in all lower-case; should be `$this->login_model->` – Sparky May 24 '16 at 18:39
  • @Sparky I said the same thing in the comments but OP says it's fine. Looking at OP's model code I identified that the return variable is not always an object. – MonkeyZeus May 24 '16 at 18:41
  • I'm not saying you didn't solve it... I'm saying the OP is using the wrong syntax. This: `application/models/Model_name.php` should be accessed like this: `$this->model_name->method();` See: http://www.codeigniter.com/user_guide/general/models.html#anatomy-of-a-model – Sparky May 24 '16 at 18:45
  • Worked like a charm. Thank you SO MUCH! I'm a first year programming student and I'm working as an intern for this company that asked me to develop this app. I wanted to learn it all the proper way but I have dead lines, so I probably skipped some important steps. Thanks once again. I'm sorry for not being able to upvote your answer, I believe I need 15 reputation points to be able to do that. Could you please explain why the controller was not able to accommodate it as you said when I simply did this: $user_id = $this->Login_model->login_user($email, $password)->user_id; – Lberteh May 24 '16 at 18:50
  • @Lberteh, if you really want to learn how to do this the right way, and have the skills to troubleshoot it when it's going wrong, then take the time to read the CodeIgniter manual. It should only take a few hours including the tutorial demos: http://www.codeigniter.com/user_guide/ – Sparky May 24 '16 at 18:58
  • I really do, I even bought a codeigniter course on Udemy, but I guess that it's missing a lot of important info. Thanks, Sparky, I'll take some time to read the manual. – Lberteh May 24 '16 at 19:02
  • @Lberteh Just so you know. The issue which you encountered is not a CodeIgniter specific issue but merely a failure to properly access a PHP variable type. If you were to `var_dump($user_info);` on a successful vs unsuccessful login attempt then you would see that the `user_id` and `first_name` properties are not available when the `login_user()` function returns a `false`. – MonkeyZeus May 24 '16 at 19:26
  • @MonkeyZeus pro tip for you - don't argue with sparky he's a very experienced codeigniter developer :-) – cartalot May 24 '16 at 20:45
  • @cartalot Thanks, see the edit in my answer for another pro-tip :-) – MonkeyZeus May 24 '16 at 21:25
  • @MonkeyZeus I sincerely appreciate your diligence. But I have gotten bitten by this kind of naming inconsistency and it can be very time consuming. So my suggestion is that there are a few rules where you have to say to beginners - do it this way. Load the model this way, and call it this way. The other consideration is that you are not just answering this one persons question. Teaching how to do it the wrong way doesn't help anyone. Stack is the goto resource for coding questions and these posts will be around for years to come. – cartalot May 24 '16 at 21:40
  • @MonkeyZeus I understand that we conventionally name the model in lower case, and that's what I did at first. But for some weird reason, I had to change my models and controllers to upper case first letter or it wouldn't work once I merged it from localhost to online server. I have no idea why though. – Lberteh May 25 '16 at 14:44
  • @Lberteh Very likely, it has something to do with [filesystem case-sensitivity](http://stackoverflow.com/questions/17006340/codeigniter-web-app-is-not-working-with-the-linux-but-here-on-windows-is-fine) – MonkeyZeus May 25 '16 at 14:49