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.