Destroying CodeIgniter Sessions when closing the browser

I’m really enjoying using CodeIgniter, it’s an excellent framework and has a side benefit of helping to structure your files neatly.

I’ve been working on my first login form using it, and whilst there are lots of tutorials going through this common process I encountered a “problem” with the CodeIgniter sessions persisting when the user closed their browser.

A bit of searching around and I came up with a few recommendations.

The first is to set the $config[‘sess_expiration’] to 0

This however has the effect of creating a cookie which actually lasts 2 years due to code in the system/libraries/Session.php file

The next was to set the $config[‘sess_expiration’] to -1

This just didn’t work for me full stop. When I logged in it saved the session variable then when I went to the next page it had disappeared.

The solution I have used came from this page.

http://codeigniter.com/forums/viewthread/70036

It’s pretty quick to implement the code and you can have control over the expiration time as well as choosing whether your cookie should be persistent.

And now it’s implemented I intend to copy the Session.php file into every project I create that requires sessions. I can handle having to paste in one line of code to the config file.

This entry was posted in Web Development and tagged , , . Bookmark the permalink.

3 Responses to Destroying CodeIgniter Sessions when closing the browser

  1. Jo says:

    Hi Ian,

    the thread you linked to at the CI forums does require a login. It would really help me out if you could actually post your solution?

    Your help is really appreciated!

    Best, Jo

  2. ian says:

    Hi Jo,

    I’ve just tried to access the post on the forum and it seems it is no longer working.

    I’ll see if I can find the code from one of my projects and if so I’ll post it here.

    Thanks

    Ian

  3. ian says:

    Hi Jo,

    Here is the Session.php file to replace system/libraries/Session.php

    http://ianluckraft.co.uk/wp-content/uploads/2012/08/Session.php_.txt

    Then you can add

    $config[‘sess_persistant_cookie’] = FALSE;

    to application/config/config.php set to FALSE sessions are destroyed when you close the window.

Leave a Reply

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