CodeIgniter Forums
Sessions, not readable in constructor. - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Sessions, not readable in constructor. (/showthread.php?tid=55931)



Sessions, not readable in constructor. - El Forum - 11-17-2012

[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.


Sessions, not readable in constructor. - El Forum - 11-17-2012

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

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

In the constructor?


Sessions, not readable in constructor. - El Forum - 11-17-2012

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


Sessions, not readable in constructor. - El Forum - 11-17-2012

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