EllisLab text mark
Advanced Search
     
Call to a member function ... on a non-object
Posted: 14 April 2012 10:33 AM   [ Ignore ]
Joined: 2012-04-14
13 posts

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:

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

Ok. So here is “pages.php”:

<?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:

$this->login_model->login(); 

And finally the model file “login_model.php”:

<?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:

$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

 
Posted: 14 April 2012 11:14 AM   [ Ignore ]   [ # 1 ]   [ Rating: 0 ]
Joined: 2010-10-18
25 posts

try class login_model or $this->Login_model->login(); using the first letter capitalized

 
Posted: 14 April 2012 11:20 AM   [ Ignore ]   [ # 2 ]   [ Rating: 0 ]
Joined: 2011-11-04
244 posts

Did you load the model?

 
Posted: 14 April 2012 11:32 AM   [ Ignore ]   [ # 3 ]   [ Rating: 0 ]
Joined: 2012-04-14
13 posts
Samus - 14 April 2012 11:20 AM

Did you load the model?

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

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

Thanks. smile


BiB

 
Posted: 06 October 2012 06:30 AM   [ Ignore ]   [ # 4 ]   [ Rating: 0 ]
Joined: 2012-10-06
2 posts

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

<?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

<?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');
  
}  
 } 

}

?> 

ci_admin.php

<?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;
  
}
 }


$config['encryption_key''userdata';
$autoload['libraries'= array('database','Form_validation','Pagination','Session'); 
 
Posted: 06 October 2012 07:14 AM   [ Ignore ]   [ # 5 ]   [ Rating: 0 ]
Joined: 2011-10-25
62 posts

try auto loading your libraries with all lowercase chars.

 
Posted: 06 October 2012 09:03 AM   [ Ignore ]   [ # 6 ]   [ Rating: 0 ]
Joined: 2012-10-06
2 posts

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.

 
Posted: 06 October 2012 11:23 AM   [ Ignore ]   [ # 7 ]   [ Rating: 0 ]
Joined: 2011-10-25
62 posts

I see you are not calling the constructor of CI_controller

Change the constructor from

function __construct()
{
    $this
->is_logged_in();

To this

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

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