1

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;
Cœur
  • 37,241
  • 25
  • 195
  • 267
Stuh_blue
  • 171
  • 2
  • 11
  • Of course codeigniter "actually" uses sessions. It can store the session data in a cookie or a database. In the case of a database, you will still have a cookie that contains the ID of the session so it can match it against the database. – Chris G. Jun 06 '13 at 16:42

3 Answers3

3

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

Erman Belegu
  • 4,074
  • 25
  • 39
1

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!

Stuh_blue
  • 171
  • 2
  • 11
0

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.

Community
  • 1
  • 1
Chris G.
  • 3,963
  • 2
  • 21
  • 40
  • "Set to zero for no expiration." from the codeigniter config file comments regarding that parameter. – Julien Jun 06 '13 at 16:45
  • Please see http://stackoverflow.com/questions/3684620/is-possible-to-keep-session-even-after-the-browser-is-closed – Chris G. Jun 06 '13 at 16:45
  • Yeah 0 means codeigniter sets it to 2 years which I can see correctly in IE's debug tools as correctly working. I have tried 0 and an actual number though incase it's a bug – Stuh_blue Jun 06 '13 at 16:46
  • @ChrisG. - so just to check. Codeigniter as far as I can see is storing a session id inside a cookie and also the database. I had thought that because I now have a cookie set it isn't using php style sessions. From what you're saying thats not right and it is still using a php session and if the php session id doesn't match the cookie session id it rgenerates it? – Stuh_blue Jun 06 '13 at 16:55
  • I'm actually trying to emulate the issue this guy is having http://stackoverflow.com/questions/14359806/why-codeignitor-does-not-finish-the-session-after-closing-the-browser?rq=1 – Stuh_blue Jun 06 '13 at 16:57
  • When you create a session in CI with database sessions enabled, it creates a session ID and stores a row in the sessions table. Then it creates a cookie that contains that session ID so that it can match the cookie to the session in between browser requests. – Chris G. Jun 06 '13 at 16:57
  • CI does not use PHP native sessions afaik. It has its own implementation. `\system\libraries\Session.php` – Chris G. Jun 06 '13 at 17:05