Welcome Guest, Not a member yet? Register   Sign In
Sessions, not readable in constructor.
#1

[eluser]Unknown[/eluser]
Hi!

I got a problem with sessions. The problem is that the sessions is not readable in a library constructor.

My autoload:
Code:
$autoload['libraries'] = array(
'database',
'layout',
'session',
'auth',
'error'
);

My Auth class (where the problem occurs):
Code:
public function __construct()
{
$this->_ci =& get_instance();
$this->_ci->load->library('session'); // Just to be sure. It may not be in the autoload.
// Loads other stuff.

$this->print_session(); // Does not print anything! $session === NULL.
}

public function print_session()
{
$session = $this->_ci->session->userdata($this->_ci->config->item('auth_session_name'));
echo $session;
}

My controller (where it works!):
Code:
class Users extends MY_Controller {
public function login(){
  $this->auth->print_session(); // Prints the content of the session!
}
}


So, why can't I get the session data in the constructor, but I can any time later in the code?
It works in the Auth class if I put it later than the constructor. Like in the destructor.
#2

[eluser]Mirge[/eluser]
What happens when you:

Code:
print_r($this->_ci->session->all_sessiondata());

In the constructor?
#3

[eluser]Unknown[/eluser]
Oh, all_userdata() worked. My bad, ofc it was the config file that was not loaded!
Thank you.
#4

[eluser]Mirge[/eluser]
Always something simple :o)




Theme © iAndrew 2016 - Forum software by © MyBB