Killing session on browser close in Codeigniter

20 Aug
2009

It may be well known to some people how to kill sessions on browser close but the default config in codeigniter doesn’t provide so. In lot of application which has admin interface, we don’t want browser to store the session. So it needs to kill the session when browser is closed. Example behavior can be found in yahoo email page.

So how do we do that in Codeigniter ? It’s simple, although this feature is not well commented or documented. Ok, now open system/application/config/config.php file

And find the settings called $config['sess_expiration'] under the section Session Variables and put the value like this


$config['sess_expiration']        = -1;

That’s it. Now your session will be killed in the event of browser closing.

6 Responses to Killing session on browser close in Codeigniter

Avatar

Zoran

August 29th, 2009 at 7:03 pm

Hmm… you sure about this? I will have a look now and try it, cause i have been mucking around with the session class and i think it’s the most complete class in CI, very useful, but will let you know if this tip of yours works well :)

Avatar

Zoran

August 29th, 2009 at 7:23 pm

Nope, it doesn’t work, it wont let me login at all if i put the sess_expiration to -1 and if have been logged in and try to set it to -1 while i am logged in, it logs me out.

Avatar

Mahbub

August 29th, 2009 at 11:11 pm

@Zoran : It works for me. I have used database to handle the session. If I’m logged in, i can browse the pages fine but when the browser closes, it doesn’t. Are you using db to handle the sessions ?

Avatar

lexa

August 31st, 2009 at 10:46 am

I use a database. It does not work!

Avatar

riz

May 13th, 2010 at 7:02 pm

It does not work for me too.

Avatar

shamim

May 20th, 2010 at 2:44 pm

If you set $config['sess_expiration'] = -1; then you will not permit to pass the login page.
so if you have any update then please give so..

Comment Form

top