Clearing dead sessions from the database

You may have found that CodeIgniter sometimes does not successfully remove sessions from the sessions table in the database. Unused sessions are instead cleared of their user_data field, but that the row remains in the sessions table.

I have often had to write specific database queries to clear sessions. I usually put these queries in the My_Controller class(refer to the Extending your controllers recipe in this chapter); however, I have recently begun to use Hooks to perform this. Then it's always working in the background and I don't need to think about it.

Getting ready

We're going to amend the following file:


Open the /application/config/config.php file ...

Get CodeIgniter 2 Cookbook now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.