Jump to content

Codeigniter session problem


c_pattle

Recommended Posts

I have a library called "Layout" which controls the main layout for my site.  In my header method I have this code that checks to see if the "loggedin" variable is set and then loads a view depending on the outcome. 

 

$loggedin = 0;
        if($loggedin)
        {
            $aData['myAccount'] = '<div class="my_account">';
            $aData['myAccount'] .= $this->load->view('account/myaccount', '', true);
            $aData['myAccount'] .= '</div>';
        }
        else
        {
            $aData['myAccount'] = '<div class="login_or_register">';
            //Assign the login view to the $aData array for display
            $aData['myAccount'] .= $this->load->view('account/login', '', true);
            $aData['myAccount'] .= '</div>';
        }

 

This works but what I want to do is to check if a session variable is set.  However it's not possible to load the session library from within another library.  Is there a way round this problem?

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/252734-codeigniter-session-problem/
Share on other sites

However it's not possible to load the session library from within another library.

 

Sure it is.

$CI =& get_instance();

$CI->load->library('session');

 

Alternatively you can autoload the session library in the application/config/autoload.php file.

Thanks.  I have autoloaded the session and I have the following code

 

$this->ci =& get_instance();
$this->ci->load->library('session');

 

but I still get the error

 

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

 

Is there something I'm doing wrong?

You then need to use it via $this->ci->session instead of $this->session

 

EDIT: Wait, so you autoloaded the session?

 

In that case, while in another library you need to take the same steps but you don't need to load it. So in another library:

$CI =& get_instance();

$CI->session->userdata();

  • 2 months later...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.