Tuz
Tuz

Reputation: 119

Codeigniter 3 - Session not working

I have recently updated form 2.2.x to 3.0.0 with following the update procedure from codeigniter's website. I have having real issues with the new session library - heres the issue.

We have a login section which dependant on the subdomain and user/pass credentials will give you certain privileges from ADMIN / RESELLER / CLIENT / USER

In order to determine the correct privileges for the user we have built a customer LIBRARY (location:application/library) which we have called Session_management, this library DOES NOT extend the core SESSION driver/library and never has and has no extension to another class, this library is also auto-loaded, prior to CI 3.0.0 everything was working fine.

First this the Session_management does is __construct()

$this->CI =& get_instance();
$this->CI->load->model('users');
$this->CI->load->model('clients');
$this->CI->load->model('sessions');
$this->CI->load->driver('session');
$this->CI->load->library('password_hash');
$this->CI->load->helper('url');

$this->users = $this->CI->users;
$this->clients = $this->CI->clients;
$this->sessions = $this->CI->sessions;
$this->session = $this->CI->session;
$this->password_hash = $this->CI->password_hash;

Prior to CI 3.0.0 I has no issues in using the

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

but for some unknown reason (to me) I HAVE to load it through the driver

$this->CI->load->diver('session');

if someone could explain why I am having to do it this way that would be great.

When a user submits their user/pass credentials a CONTROLLER session/signin is requested which runs firstly form validation, providing everything is successfully, the Session_management login method is called.

$success = $this->session_management->login
  ($this->input->post('email'), $this->input->post('password'));

In the LOGIN method in the Session_management class a bunch of sessions are set using

$this->session->set_userdata();

$this->session->set_userdata('user_id', 0);
$this->session->set_userdata('user_name', '');
$this->session->set_userdata('client_id', 0);
$this->session->set_userdata('client_administrator', 0);
$this->session->set_userdata('reseller_administrator', 0);

However when I var_dump() the session for all its session data it has NOTHING and I can't why this is, no session data is there except my protected fields form the config, which I have double checked and triple checked and are working fine, my sess_save_path is storing the session files correctly, and the rest of the sess configs are also correct.

$config['sess_driver'] = 'files';
$config['sess_cookie_name'] = 'ci_session';
$config['sess_expiration'] = 7200;
$config['sess_save_path'] = '/Users/******/Sites/********/tmp';
$config['sess_match_ip'] = FALSE;
$config['sess_time_to_update'] = 300;
$config['sess_regenerate_destroy'] = FALSE;

This is a development website on my local OS X iMac, as I say prior to CI 3.0.0 everything was working fine.

Just to add before I get reply's saying "you need to use"

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

I have "HAD" to load it as a driver, I don't have a choice, I have read the documentation and have seen how to initialise the session library.

If I do attempt to load it as a library

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

This is what I get

A PHP Error was encountered

Severity: Notice
Message: Undefined property: Session::$session
Filename: libraries/Session_management.php
Line Number: 38

For your ref: line 38 is:

$this->session = $this->CI->session;

This is after trying to load the session library

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

Also to add to this message re: Database sessions / File session. Database sessions were my first option, a have revamp the database columns and indexes to suit CI 3.0.0 as the documentation mentions and session were and are storing in the database table when I change my config to use the database, however reading the performance differences between File sessions against Database sessions under high load, File sessions will out perform database session and since the website / platform I am creating will be under high load, database sessions aren't the way forward.

As a note: The website runs under a subdomain.domain.*** structure where subdomain is registered as a company name upon a company registration i.e

mycompany.mywebsiteurl.com
anothercompany.mywebsiteurl.com

As previously mention - prior to my update to CI 3.0.0 it was working fine.

Might I also add: I have checked my log files, I am running tail -f for live log updates - and it don't see any log issues.

Any help, information or anything that could possible put me in the right direction would be appreciated.

I have also posted on the CI forum.

Upvotes: 2

Views: 5206

Answers (1)

Tuz
Tuz

Reputation: 119

A fresh copy of CI 3.0.0 with necessary files and configs transferred across didn't solve my problem/issue.

What seems to be the whole cause of the problem was the fact I had a controller call Sesssion.php which is named the same as the Driver file.

When CI (Codeigniter) calls

$this->load->library('session')

there are a few checks done before CI knows that you want to actually load the CI_Session class ... class_exists('Session') returns TRUE and it stops there in order to avoid a fatal error.

Hope this helps others too.

Upvotes: 2

Related Questions