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.

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.

3 thoughts on “Destroying CodeIgniter Sessions when closing the browser”

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



Leave a Reply

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