Good work Oscar!
I need some help. I’m using OBSession on a fresh install of CI 1.5.2 and PHP 4.4.4
I am trying to create a library that will give me simple login functionality (I realize there are a ton of these libraries, I am just looking for a real, real simple solution).
My problem is that when I try to set session data inside my library, it looks like I end up with two instances of Session data. Let me give you the related code to see if you can see what is happening.
My library is call Simplelogin.
I am autoloading ‘database’, ‘session’, ‘simplelogin’
In my main controller, my index function is as follows:
function index()
{
//Set session data using the controller instance
$this->session->set_userdata('my_controller', 'controller_value');
//Set session data using the library instance
$this->simplelogin->simple_test();
print_r($this->session->userdata);
echo '<br /><br />'';
print_r($this->simplelogin->CI->session->userdata);
}
In my Simplelogin library:
My initialization looks like this:
function Simplelogin()
{
$this->CI =& get_instance();
}
My simple_test function is as follows:
function simple_test() {
$this->CI->session->set_userdata('my_library', 'library_value');
}
I delete cookies, then run the code and the first time I get this:
Array ( [my_controller] => controller_value )
Array ( [my_library] => library_value )
And every other time, I get this:
Array ( [my_library] => library_value [my_controller] => controller_value )
Array ( [my_library] => library_value )
I am expecting them to be the same, but obviously they are not. What am I missing? How would I set session data in a library and then call that session data in my main controller? Or vice-versa (on the second reload the session data gets added to the controller instance, but the library instance never sees the controller values)?
One work around that I have thought of that works is to create alias functions in my simplelogin library that would point to the session functions. In other words, every time I set_userdata I would do it through my simplelogin library (), which does not seem right (obviously I am doing something wrong). I guess another option would be to just set_userdata like this:
$this->simplelogin->CI->session->set_userdata('my_controller', 'controller_value');
but that doesn’t seem right either.
I don’t think this is an OBSession issue (I had the exact same problem with DB_Session), I just thought you might be able to quickly see what I am doing wrong.
Any thoughts or help would be greatly appreciated.