Skip to content Skip to navigation

Sessions Table cleanup

Posted by: 

I recently stumbled across a problem with the way one of my Drupal sites was handling session data that conceivably could have gunked up the DB to the point of failure, so please read on if you have a heavy traffic site, or one using Organic Groups.

I would like to thank Marco Wise, John Robertson, and John Bickar for working on this and coming up with the fix.

It is a known issue that, depending on one's local PHP settings, a drupal sites's sessions table may never get cleaned up (truncated). I logged in to phpmyadmin to find that mine had 117,000 records!! (you might want to look at yours.) A little more digging showed that OG was responsible for most of it, fwiw.

The fix requires a change to the PHP settings section of settings.php near line 127, located in /sites/default/
(from JB:)

ini_set('arg_separator.output',     '&');
ini_set('magic_quotes_runtime',     0);
ini_set('magic_quotes_sybase',      0);
ini_set('session.cache_expire',     10800);
ini_set('session.cache_limiter',    'none');
ini_set('session.cookie_lifetime',  10800);
ini_set('session.gc_maxlifetime',   10800);
ini_set('session.save_handler',     'user');
ini_set('session.use_only_cookies', 1);
ini_set('session.use_trans_sid',    0);
ini_set('url_rewriter.tags',        '');
ini_set('session.gc_probability', 1);

Paste in the above code to replace what is already there. The last line is the only new line added to the existing code, other changes modify variable settings. The value '10800' corresponds to the number of seconds before an action takes place, so if you want your cookies to last longer than 3 hours, pick a higher number.