Welcome Guest, Not a member yet? Register   Sign In
writing a library that uses other libraries
#1

[eluser]dadamssg[/eluser]
I'm trying to write a library that holds a function that simply checks if the user is logged in or not and return true/false. I need to load the session library in my library. I referenced the session class like the user guide says but it still isn't being referenced right. I still have an error on the line i'm setting $username with. Any see what im doing wrong?

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

class Loginclass {

    public function logged_in()
    {
        $CI =& get_instance();
        $CI->load->library('session');
        
        $username = $this->session->userdata('username');
        $logged_in = $this->session->userdata('logged_in');
        if(empty($username)){$value = FALSE;}
        elseif(!is_numeric($username)){$value = FALSE;}
        elseif($logged_in !== TRUE){$value = FALSE;}
        else{$value = TRUE;}
        return $value;
    }
}

?>

my relavent controller function
Code:
public function log()
    {
        $this->load->library('loginclass');
        if($this->loginclass->logged_in())
        {
            echo "You are logged in.";
        }
        else
        {
            echo "You are not logged in.";
        }
    }
#2

[eluser]dadamssg[/eluser]
fixed it. I wasn't using $CI when i was referencing my session variables

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

class Loginclass {

    public function logged_in()
    {
        //load codeigniter object
        $CI =& get_instance();
        
        //load session library to use
        $CI->load->library('session');    
        
        //set values to check
        $username = $CI->session->userdata('username');
        $logged_in = $CI->session->userdata('logged_in');    
        
        //check values
        if(empty($username)){$value = FALSE;}
        elseif(!is_numeric($username)){$value = FALSE;}
        elseif($logged_in !== TRUE){$value = FALSE;}
        else{$value = TRUE;}
        
        
        return $value;
    }
}

?>
#3

[eluser]C. Jiménez[/eluser]
You can't call $this->session because it doesn't exist.

You have referenced your CI objec in $CI :
$CI->load->library('session');

so use $CI instead $this

Code:
class Loginclass {

    public function logged_in()
    {
        $CI =& get_instance();
        $CI->load->library('session');
        
        //$username = $this->session->userdata('username');
        //$logged_in = $this->session->userdata('logged_in');
        $username = $CI->session->userdata('username');
        $logged_in = $CI->session->userdata('logged_in');
      
        if(empty($username)){$value = FALSE;}
        elseif(!is_numeric($username)){$value = FALSE;}
        elseif($logged_in !== TRUE){$value = FALSE;}
        else{$value = TRUE;}
        return $value;
    }
}

if you want to use your $CI object in more than one method I reccomend you to save it in a private var of your class and fill it with your referenced CI object on class's constructor.

Hope it helps!




Theme © iAndrew 2016 - Forum software by © MyBB