CodeIgniter Forums

Full Version: Retrieving session->userdata() into my personal library
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]Ngulo[/eluser]
Hi all guys Smile

i'm trying to retrieve session data by my own library.

library is called Settings.php

and i put it to application/libraries as explained into user guide.

than i just put into a little bite of code like as shown:
Code:
<?php

if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Settings {

     function setsitelanguage()
    {
        
        if($this->session->userdata('lang'))
        {
            $this->config->config['language'] = $this->session->userdata('lang');
        }
        else
        {
          $this->config->config['language'] = "en";
        }
        
    }
    


}//end class Settings.php
i loaded session library and this one library by default in the autoloader.php!

the problem is that i want to check the session userdata, into this library but i always receive this error:
Code:
Fatal error: Call to a member function userdata() on a non-object

the scheme is as shown:

1) first, i set userdata into my auth controller and redirect to home controller
Code:
class Auth extends Controller {

function usersetlang()
    {
            
               $lang = $this->uri->segment(3);
            
                foreach($this->config->config['sitelangs'] as $data['lang'] )
                {
                   if($lang == $data['lang'])
                    {
                        $this->session->set_userdata('lang',$lang);
                       redirect('home/home');
                    }
                }
                
        }
2) second , home controller try to configure site language by doing this
Code:
class Home extends Controller {

    function Home()
    {
        parent::Controller();
               $this->settings->setsitelanguage();
              
    }

3) i take the error Tongue

i really can't find a solution, cause it seems that the session library is never loaded by autoloader.php , and than i can't access session object!!! :/

cansomeone help me?

really thanks Wink

El Forum

[eluser]WanWizard[/eluser]
Are you saying that you get errors on all calls to $this->session in all examples? Or only in the library?

For the library, try
Code:
$CI =& get_instance();
if ( $CI->session->userdata('lang') ) echo "Hurray!";

El Forum

[eluser]Ngulo[/eluser]
wowwwwww!!

bingo Smile

you fix it!!
thanks really thanks

the problem was only for my library Wink