1st June 2011

Codeigniter Sessions: Revisited

Back in 2009 I posted an article on Codeigniter sessions and how certain versions of Internet Explorer got a bit unsuck when authenticating a valid session. The post has probably had more traffic than the rest on my site combined!

The fix was to use a different library to user native PHP sessions as opposed to Codeigniters session class.

3 years on, I'm a little older and a little wiser so I thought it might help to post a far easier solution that so far hasn't caused me any headaches. In the config file set change the following:

$config['sess_use_database'] = true;

For this to work, you’ll also need a database:

CREATE TABLE IF NOT EXISTS  `ci_sessions` (
session_id varchar(40) DEFAULT '0' NOT NULL,
ip_address varchar(16) DEFAULT '0' NOT NULL,
user_agent varchar(50) NOT NULL,
last_activity int(10) unsigned DEFAULT 0 NOT NULL,
user_data text DEFAULT '' NOT NULL,
PRIMARY KEY (session_id)
);

In Codeigniter 2, sessions also require an encryption key. If you're unsure how to generate them, you can get someone to create on for you.

$config['encryption_key'] = 'YourEncryptionKey';

Of course, all the documentation for the sessions class can be found on the excellent Codeigniter user guide, plus I believe the old fix still works in the most recent version.