Welcome Guest, Not a member yet? Register   Sign In
Database sessions and garbage collection
#1

Hello CI community,
First of all thank you for the great framework and the support afterwards.

I've recently updated our application to CI3 and almost everything is working as expected except the sessions.
First, let me describe my situation..

My session configuration:
PHP Code:
$config['sess_driver'] = 'database';
$config['sess_cookie_name'] = 'dk_session';
$config['sess_expiration'] = 7200;
$config['sess_save_path'] = 'sessions';
$config['sess_match_ip'] = FALSE;
$config['sess_time_to_update'] = 0// !!
$config['sess_regenerate_destroy'] = FALSE

It's worth mentioning that I have an extra column `user_id`(int 20) in the `sessions` table to easily identify my online users.

The problem:
Expired(garbage) sessions are never deleted(collected) from the database -> thus making 'expired' users appear online and only 2 days after going to production i already have the sessions table with 5K+ records..

So no garbage collection for database sessions since CI3 or i'm missing something?

Thanks in advance for your help!
Reply
#2

(03-08-2015, 09:34 AM)kozzleto Wrote: It's worth mentioning that I have an extra column `user_id`(int 20) in the `sessions` table to easily identify my online users.

This would be the first thing I would look at. My best guess would be that whatever you are doing to maintain the user_id in the session table is interfering with the session driver's maintenance of the sessions.

I would recommend using a reference table (user_id, session_id) for this, instead (with the user_id set as a unique value so you don't end up with multiple session_id entries per user). You could then use an inner join when retrieving the online users to ensure you only get session_id values which are in both tables.
Reply
#3

Ok, I finally found what the problem is.
PHP had session.gc_probability set to 0. Don't know really how that happened, maybe the latest php5 package from Ubuntu modified it, as I made apt-get upgrade about 2 weeks ago and php5 was also inside.

System: Ubuntu Server 14.04.2
PHP 5.5.9-1ubuntu4.6

Here's more info about PHP's session garbage collection in case somebody needs it.
Quote:session.gc_probability in conjunction with session.gc_divisor is used to manage probability that the gc (garbage collection) routine is started. Defaults to 1

Quote:session.gc_divisor coupled with session.gc_probability defines the probability that the gc (garbage collection) process is started on every session initialization. The probability is calculated by using gc_probability/gc_divisor, e.g. 1/100 means there is a 1% chance that the GC process starts on each request. session.gc_divisor defaults to 100.
Source: http://php.net/manual/en/session.configu...robability

mwhitney thanks for your suggestion, but I really think that the method i'm using to find online users is more appropriate.
As of the maintenance of the `user_id` field I'm doing the following on user login:

PHP Code:
       // delete user's old sessions
 
       $this->db->delete('sessions', array(
 
           'user_id' => $id
        
));

 
       // fill `sessions`.`user_id` of the new one
 
       $this->db->where('id'session_id())
 
           ->update('sessions', array(
 
               'user_id' => $uid
            
)); 
Reply




Theme © iAndrew 2016 - Forum software by © MyBB