Stuh_blue
Stuh_blue

Reputation: 171

Codeigniter session regenerated after browser close

I have a situation where by I need to store information into codeigniters session -> be able to close the browser window -> then open up the browser window and for it to recover all the items I stored in a session. I'm aware codeigniter doesn't actually use sessions and instead cookies so this should be really easy.... but it's not.

In FF / Safari / Chrome this works fine but in IE 9 when I close and then re-open the window it regenerates the session id and doesn't pull back the data.

For example this simple code:

$this->CI->session->set_userdata('username','My Username');
echo $this->CI->session->userdata('username');

when run the first time outputs as expected. I then comment out line 1 and refresh the screen and the session data is again displayed correctely.

Now I close the browser window and open it back up to the same page, again with line 1 commented out. It can't find the session data and the session id is now changed.

I have sessions saving to the database in codeigiter and my session config looks like:

$config['sess_cookie_name']     = 'bcsession';
$config['sess_expiration']      = 0;
$config['sess_expire_on_close'] = FALSE;
$config['sess_encrypt_cookie']  = TRUE;
$config['sess_use_database']    = TRUE;
$config['sess_table_name']      = 'ci_sessions';
$config['sess_match_ip']        = FALSE;
$config['sess_match_useragent'] = FALSE;
$config['sess_time_to_update']  = 300;

Upvotes: 1

Views: 2536

Answers (3)

Erman Belegu
Erman Belegu

Reputation: 4079

You need to set config.php:

$config['sess_expiration'] = write the number of seconds

This is the number of seconds you would like the session to last. The default value is 2 hours (7200 seconds). If you would like a non-expiring session set the value to zero: 0.

$config['sess_expire_on_close'] = TRUE;

Whether to cause the session to expire automatically when the browser window is closed. FALSE is default, but you need to change.

For more, please read in details: http://www.codeigniter.com/userguide2/libraries/sessions.html

Thanks a lot

Upvotes: 3

Stuh_blue
Stuh_blue

Reputation: 171

I've found the solution to this in case it effects anyone else.

CI sessions are infact cookies as my logic above however i can't believe I didn't spot it but IE has the setting to delete browser history/cookies/cache upon exit. This was ticket.

Why is it you always look at the most complex reason first and not the most obvious!

Upvotes: 1

Chris G.
Chris G.

Reputation: 3981

I believe sess_expiration may require a non-zero value to solve your problem. Try that, delete your session in the database table, and give it another shot.

From Is possible to keep session even after the browser is closed?:

Use session_set_cookie_parameters() to give the session cookie a non-zero lifetime before starting the session, or set session.cookie_lifetime to non-zero.

Codeigniter will call that function on $config['sess_expiration'] so try setting it to a non-zero value.

Upvotes: 0

Related Questions