Welcome Guest, Not a member yet? Register   Sign In
call to undefined property
#1

[eluser]elmne[/eluser]
When i access "admin/admin/login" whereby "admin" is a folder and the second "admin" is the controller and "login" is the function within the contoller, this is what i get,


Quote:A PHP Error was encountered
Severity: Notice

Message: Undefined property: Admin::$session

Filename: libraries/auth.php

Line Number: 103


Fatal error: Call to a member function userdata() on a non-object

But session is loaded in


Code:
function Auth()
    {
        $this->CI->load->library('session');
        $this->CI->load->database();
        $this->CI->load->helper('url');
    }


and line 103 is this: if ($this->CI->session->userdata('logged_user') == FALSE)

Code:
/**
     *
     * Checks if a user is logged in
     *
     * @access    public
     * @return    boolean
     */
    function logged_in()
    {
        if ($this->CI->session->userdata('logged_user') == FALSE)
        {
            return FALSE;
        }
        else
        {
            return TRUE;
        }
    }


My auth.php in libraries is as follows;

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

class Auth {

    var $CI = null;
    
     // get the CI Super object
    function __construct(){
        $this->CI =& get_instance();
    }

    function Auth()
    {
        $this->CI->load->library('session');
        $this->CI->load->database();
        $this->CI->load->helper('url');
    }

    function process_login($login = NULL)
    {
        // A few safety checks
        // Our array has to be set
        if(!isset($login))
            return FALSE;
    
        //Our array has to have 2 values
        //No more, no less!
        if(count($login) != 2)
            return FALSE;
    
        $username = $login[0];
        $password = $login[1];
    
        // Query time
        $this->CI->db->where('username', $username);
        $this->CI->db->where('password', $password);
        $query = $this->CI->db->get('user_account_staff');
    
        if ($query->num_rows() == 1)
        {
            // Our user exists, set session.
            $this->CI->session->set_userdata('logged_user', $username);
            return TRUE;
        }
        else
        {
            // No existing user.
            return FALSE;
        }
    }

    function redirect()
    {
        if ($this->CI->session->userdata('redirected_from') == FALSE)
        {
            redirect('/admin');
        } else {
            redirect($this->CI->session->userdata('redirected_from'));
        }
    
    }



    /**
     *
     * This function restricts users from certain pages.
     * use restrict(TRUE) if a user can't access a page when logged in
     *
     * @access    public
     * @param    boolean    wether the page is viewable when logged in
     * @return    void
     */
    function restrict($logged_out = FALSE)
    {
        // If the user is logged in and he's trying to access a page
        // he's not allowed to see when logged in,
        // redirect him to the index!
        if ($logged_out && $this->logged_in())
        {
            redirect('/admin');
        }
    
        // If the user isn' logged in and he's trying to access a page
        // he's not allowed to see when logged out,
        // redirect him to the login page!
        if ( ! $logged_out && ! $this->logged_in())
        {
            $this->CI->session->set_userdata('redirected_from', $this->CI->uri->uri_string()); // We'll use this in our redirect method.
            redirect('/admin/login');
        }
    }
    
    /**
     *
     * Checks if a user is logged in
     *
     * @access    public
     * @return    boolean
     */
    function logged_in()
    {
        if ($this->CI->session->userdata('logged_user') == FALSE)
        {
            return FALSE;
        }
        else
        {
            return TRUE;
        }
    }


    function logout()
    {
        $this->CI->session->sess_destroy();
    
        return TRUE;
    }



}
// End of library class
// Location: system/application/libraries/Auth.php


What's the cause of the error?
#2

[eluser]mddd[/eluser]
You have both a __construct and a Auth method in your class. Only one of them will be executed when the class is loaded.
It seems that __contruct is executed but Auth is not. Resulting in the absence of the session library.
Put the two methods them together and check if that resolves the issue.




Theme © iAndrew 2016 - Forum software by © MyBB