Killing session on browser close in Codeigniter

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

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

Author's gravatar

Md. Mahbubur Rahman

14 thoughts on “Killing session on browser close in Codeigniter

    Author's gravatar

    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 🙂

    Author's gravatar

    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.

    Author's gravatar

    @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 ?

    Author's gravatar

    I use a database. It does not work!

    Author's gravatar

    It does not work for me too.

    Author's gravatar

    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..

    Author's gravatar

    yep – $config[‘sess_expire_on_close’] = TRUE;

    Author's gravatar

    This is not working for me.. This is totally wrong solution… Please updates as required…

Your email address will not be published. Required fields are marked *