Welcome Guest, Not a member yet? Register   Sign In
Call to a member function ... on a non-object
#1

[eluser]BigBonsai[/eluser]
Hello,

I have been struggling to get my bearings on CI now for a few days. Things have gone fairly well. I have overcome the dust on the user guide (some outdated stuff in there) and now I am thoroughly stuck. After reading this forum for two hours I still cannot get anywhere, although this problem has been discussed before. I guess my understanding of OOP is not good enough. Well, let's see. This is my error message:

Code:
Fatal error: Call to a member function login() on a non-object in [...]\application\controllers\pages.php on line 46

Ok. So here is "pages.php":

Code:
<?php

class Pages extends CI_Controller {

    public $data = array('subview' => 'Oops... no subview');

    public function __construct() {
parent::__construct();
session_start();
$this->load->helper('url');
$this->load->helper('language');
$this->load->language('general', 'german');
    }

    [...]

    public function signin() {
$this->load->helper('form');
$this->load->library('Form_validation');

$this->form_validation->set_rules('login_nick', 'Nick', 'required');
$this->form_validation->set_rules('login_password', 'Password', 'required');

if ($this->form_validation->run() === FALSE) {
     $this->data['subview'] = 'signin';
     $this->load->view('layouts/layout', $this->data);
} else {
     $this->login_model->login();
     $this->data['subview'] = 'success';
     $this->load->view('layouts/layout', $this->data);
}
    }

}

The problem is this line:
Code:
$this->login_model->login();
And finally the model file "login_model.php":

Code:
<?php

class Login_model extends CI_Model {

    function login($nick = '', $password = '') {
//Put here for PHP 4 users
$this->CI = & get_instance();

//Make sure login info was sent
if ($nick == '' OR $password == '') {
     return false;
}

//Check if already logged in
if ($this->CI->session->userdata('user_nick') == $nick) {
     //User is already logged in.
     return false;
}

//Check against user table
$this->CI->db->where('user_nick', $nick);
$query = $this->CI->db->get_where($this->user_table);

if ($query->num_rows() > 0) {
     $row = $query->row_array();

     //Check against password
     if (hash("512", $password) != $row['user_pw']) {
  return false;
     }

     //Destroy old session
     $this->CI->session->unset_userdata();

     //Create a fresh, brand new session
     $this->CI->session->sess_create();

     //Remove the password field
     unset($row['user_pw']);

     //Set session data
     $this->CI->session->set_userdata($row);

     //Set logged_in to true
     $this->CI->session->set_userdata(array('online' => true));

     //Login was successful
     $_SESSION['logged_in'] = 1;
     return true;
} else {
     //No database result found
     $_SESSION['logged_in'] = 0;
     return false;
}
    }

}

As you can see (for whoever knows it), the login function is copied from the Simplelogin library, but ultimately I suspect it doesn't matter what it says in there, since it is not accessed in the first place.

The database connection is not included, because I have this line in the autoconfig.php:

Code:
$autoload['libraries'] = array('database', 'session');

In my humble understanding, this should take care of the database connection.

What am I overlooking/messing up for getting the above mentioned error?

Thanks a bundle in advance! Smile


BiB
#2

[eluser]Rodrigo Alexandre[/eluser]
try class login_model or $this->Login_model->login(); using the first letter capitalized
#3

[eluser]Samus[/eluser]
Did you load the model?
#4

[eluser]BigBonsai[/eluser]
[quote author="Samus" date="1334416811"]Did you load the model?[/quote]
No... *argh* Stupid me!!! And for that 3 hours...
So if another n00b like me ever runs into this: In my case the simple solution is this line
Code:
$this->load->model('login_model');
Thanks. Smile


BiB
#5

[eluser]chochoro[/eluser]
Will you help me? This is my problems:


A PHP Error was encountered

Severity: Notice

Message: Undefined property: Site::$session

Filename: controllers/site.php

Line Number: 23
Fatal error: Call to a member function userdata() on a non-object in C:\Program Files\EasyPHP-12.1\www\Ang_Hirap_Haha\application\controllers\site.php on line 23

Here are my codes:
admin.php

Code:
<?php
class Admin extends CI_Controller
{
   function index()
{
  
  $data['main_content'] = 'login_form';
  $this->load->view('template', $data);  
}

function validate_credentials()
{  
  $this->load->model('ci_admin');
  $query = $this->ci_admin->validate();
  
  if($query) // if the user's credentials validated...
  {
   $data = array(
    'username' => $this->input->post('username'),
    'is_logged_in' => true
   );
   $this->session->set_userdata($data);
   redirect('site/members_area');
  }
  else // incorrect username or password
  {
   $this->index();
  }
}


function logout()
{
  $this->session->sess_destroy();
  $this->index();
}  


}

?>

site.php
Code:
<?php

class Site extends CI_Controller
{
function __construct()
{
  $this->is_logged_in();
  
}

function members_area()
{
  $this->load->view('logged_in_area');
}

function another_page() // just for sample
{
  echo 'good. you\'re logged in.';
}

function is_logged_in()
{
  $is_logged_in = $this->session->userdata('is_logged_in');
  if(!isset($is_logged_in) || $is_logged_in != true)
  {
   echo 'You don\'t have permission to access this page. <a href="../login">Login</a>';
   die();  
   //$this->load->view('login_form');
  }  
}

}

?&gt;
ci_admin.php
Code:
&lt;?php

class ci_admin extends CI_Model
{
  function validate()
{
  $this->db->where('username', $this->input->post('username'));
  $this->db->where('password', $this->input->post('password'));
  $query = $this->db->get('admin');
  
  if($query->num_rows == 1)
  {
   return true;
  }
}


}
Code:
$config['encryption_key'] = 'userdata';
$autoload['libraries'] = array('database','Form_validation','Pagination','Session');
#6

[eluser]GrahamDj28[/eluser]
try auto loading your libraries with all lowercase chars.
#7

[eluser]chochoro[/eluser]
cases don't matter sir. i already tried that one. tsk. i wanna finish our project as fast as i can before the semester ends.
#8

[eluser]GrahamDj28[/eluser]
I see you are not calling the constructor of CI_controller

Change the constructor from
Code:
function __construct()
{
    $this->is_logged_in();
}

To this
Code:
function __construct()
{
    parent::__construct();
    $this->is_logged_in();
}

By not calling the parent constructor the CI super object is not available
#9

[eluser]soluicius[/eluser]
Hello,
I followed this tutorial( https://tutsplus.com/course/codeigniter-best-practices/ - 04-Using Third Party Libraries) but i'm stuck while securing the pages.
I think spark doesn't load ion_auth model
I get this error:
Code:
A PHP Error was encountered

Severity: Notice

Message: Undefined property: Welcome::$ion_auth

Filename: controllers/welcome.php

Line Number: 9

Fatal error: Call to a member function logged_in() on a non-object in C:\wamp\www\application\controllers\welcome.php on line


Here is my controller:
1) user.php
Code:
&lt;?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class User extends CI_Controller {

public function __construct()
{
  parent::__construct();
  
}

public function login()
{
  echo 'Login form';
}

}

/* End of file user.php */
/* Location: ./application/controllers/user.php */

2) welcome.php
Code:
&lt;?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Welcome extends CI_Controller {


public function __construct() {
  parent::__construct();

  if ($this->ion_auth->logged_in() == false) {
   redirect('user/login');
  }
}

/**
  * Index Page for this controller.
  *
  * Maps to the following URL
  *   http://example.com/index.php/welcome
  * - or -  
  *   http://example.com/index.php/welcome/index
  * - or -
  * Since this controller is set as the default controller in
  * config/routes.php, it's displayed at http://example.com/
  *
  * So any other public methods not prefixed with an underscore will
  * map to /index.php/welcome/<method_name>
  * @see http://codeigniter.com/user_guide/general/urls.html
  */
public function index()
{
  $this->load->view('welcome_message');
}
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */

and in autoload.php i have:
Code:
$autoload['sparks'] = array('ion_auth');
Any suggestions?




Theme © iAndrew 2016 - Forum software by © MyBB