• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
non-object and userdata

#1
[eluser]Kemik[/eluser]
Hello,

I'm building a login page for my app and have encountered a problem. I'm not sure why but I get Fatal error: Call to a member function on a non-object in /home/sh/public_html/project/system/application/libraries/auth.php on line 60 when the page attempts to load.

Here's the code (line below comment is 60)
Code:
function logged_in() {
        // Checks if user's session has a user_id in it
        if ($this->session->userdata('user_id') != FALSE) {
            return TRUE;
        } else {
            return FALSE;
        }
    }

Edit: Forgot to mention, session library is autoloaded.

#2
[eluser]Craig A Rodway[/eluser]
Have you loaded the session library?

#3
[eluser]Kemik[/eluser]
Yeap. Autoloaded.

The function is in an auth library which is also autoloaded. Here's the code taken from the user/login controller where the logged_in() function is called.
Code:
function login()
    {
        // Check if already logged in.
        if ($this->auth->logged_in() === TRUE) {
            redirect('user');
        }

#4
[eluser]wiredesignz[/eluser]
Fatal error: Call to a member function on a non-object suggests $this->session is not visible to your library, you should use $CI = &get;_instance(); and then $CI->session->userdata('user_id') etc.

#5
[eluser]Pascal Kriete[/eluser]
EDIT: Forgot to refresh before reading...

#6
[eluser]Kemik[/eluser]
[quote author="wiredesignz" date="1201136854"]Fatal error: Call to a member function on a non-object suggests $this->session is not visible to your library, you should use $CI = &get;_instance(); and then $CI->session->userdata('user_id') etc.[/quote]

I already do this at the start of the library:

Code:
class Auth {

    var $CI;
    
    function __construct() {
        $this->CI =& get_instance();
        log_message('debug', 'Authorization class initialized.');
    }

It's basically Erkana Auth with a few changes and additional functions.

#7
[eluser]Pascal Kriete[/eluser]
You got the instance, now you have to use it, too
Code:
$this->auth->logged_in()
//should be
$this->CI->auth->logged_in()

#8
[eluser]Kemik[/eluser]
[quote author="inparo" date="1201137407"]You got the instance, now you have to use it, too
Code:
$this->auth->logged_in()
//should be
$this->CI->auth->logged_in()
[/quote]

Thanks for the suggestion. I tried the change you said but then it gave the same error but on the $this->CI->auth->logged_in() line. I tried changing
Code:
if ($this->session->userdata('user_id')) {
// to:
if ($this->CI->session->userdata('user_id')) {

But the original error didn't change.

#9
[eluser]wiredesignz[/eluser]
don't use $this->CI in your controller, only in your library. The Controller already is part of the CI Super-object

#10
[eluser]Kemik[/eluser]
Here's the current code.

Controller - user.php
Code:
class User extends Controller
{
    function User()
    {
        parent::Controller();
        $this->load->model('user_model');
    }

    function login()
    {
        // Check if already logged in.
        if ($this->auth->logged_in() === TRUE) {
            redirect('user');
        }

        // Form inc validation and view edited out                
        
    }

Library - auth.php
Code:
<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

class Auth {

    var $CI;
    
    function __construct() {
        $this->CI =& get_instance();
        log_message('debug', 'Authorization class initialized.');
    }

    function logged_in() {
        // Checks if the user is logged in.
        if ($this->CI->session->userdata('user_id')) {
            return TRUE;
        } else {
            return FALSE;
        }
    }
}

?>


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.