Welcome Guest, Not a member yet? Register   Sign In
Load a custom library
#1

[eluser]Avril[/eluser]
Hi,

I have a made a library to check if a user is logged in or not (I'm learning CI :p).
Now the library is made and should work.

When I want to load the library in one of my controllers, I get the error:
Quote:Fatal error: Call to a member function is_logged_in() on a non-object in /Users/user/Sites/myBudget/application/controllers/login.php on line 10

This is my library file:

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


class Authenticator {

    /* Validate the users login credentials */
    function validate(){
    
        $this->load->model('membership_model');
        $query = $this->membership_model->validate();
        
        if($query){
        
            $data = array(
                         'username' => $this->input->post('username'),
                         'is_logged_in' => true
                         );
                        
            $this->session->set_userdata($data);
            return TRUE;
        
        }else{
            
            return FALSE;
        
        }
    
    }
    
    function is_logged_in(){
    
        $is_logged_in = $this->session->userdata('is_logged_in');
        
        if(!isset($is_logged_in) || $is_logged_in != TRUE){
            
            return FALSE;
            
        }else{
            
            return TRUE;
        
        }
    
    }
    
    function logout(){
    
        $logout = $this->session->sess_destroy();
        if($logout){
            
            return TRUE;
        
        }
    
    }
    
}

Don't worry for the query, the data will be send by passing an array to the object later on, I just copied it from my previous version where I checked the login directly in the controller.

This is my controller:

Code:
<?php

class Login extends Controller {
    
    var $Authenticator;
    
    function index(){
    
        $Authenticator = $this->load->library('Authenticator');
        $auth = $this->Authenticator->is_logged_in();
    
    }

}

What am I doing wrong here?
#2

[eluser]Bart Mebane[/eluser]
When you load a library, CI will automatically set up a property with the same name, all lower case. Try it this way:
Code:
<?php

class Login extends Controller {
    function index(){
    
        $this->load->library('authenticator');
        $auth = $this->authenticator->is_logged_in();
    
    }

}
#3

[eluser]Avril[/eluser]
ok cool, it's working.

But seems that I can't call a function from another library object within my custom library.

Quote:A PHP Error was encountered

Severity: Notice

Message: Undefined property: Authenticator::$session

Filename: libraries/Authenticator.php

Line Number: 42

Fatal error: Call to a member function userdata() on a non-object in /Users/user/Sites/myBudget/application/libraries/Authenticator.php on line 42

code:

Code:
function is_logged_in(){
    
        $is_logged_in = $this->session->userdata('is_logged_in');
        
        if(!isset($is_logged_in) || $is_logged_in != TRUE){
            
            return FALSE;
            
        }else{
            
            return TRUE;
        
        }
    
    }
The session library is loaded automatically, so theoretically I can use it, or must I load it again from my authenticator library?
#4

[eluser]Bart Mebane[/eluser]
You need to get an instance of the CI object in order to use the properties from a library:
Code:
function is_logged_in(){

        $CI = & get_instance();
    
        $is_logged_in = $CI->session->userdata('is_logged_in');
        
        if(!isset($is_logged_in) || $is_logged_in != TRUE){
            
            return FALSE;
            
        }else{
            
            return TRUE;
        
        }
}
See also User Guide > Creating Libraries > Utilizing CodeIgniter Resources within Your Library.




Theme © iAndrew 2016 - Forum software by © MyBB