Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How are expired codeigniter sessions cleaned up?

Experts,

I'm trying to understand how sessions are cleaned up in Codeigniter in case the app is configured to store sessions in a database table.

In my case, I have three expired sessions in that session table and one active one for the same user. The manual states: "Note: The Session class has built-in garbage collection which clears out expired sessions so you do not need to write your own routine to do it."

Hmm, so when are my 'old' session in the db session table being cleared out or am I missing something?

thanks!

like image 873
checkmate711 Avatar asked Nov 20 '12 00:11

checkmate711


People also ask

How can delete session data in CodeIgniter?

Remove Session Data In PHP, we can remove data stored in session using the unset() function as shown below. unset($_SESSION['some_name']); Removing session data in CodeIgniter is very simple as shown below. The below version of unset_userdata() function will remove only one variable from session.

How can store value in session in CodeIgniter?

php create an array to store your session data. $new_data = array( 'username' => 'martin', 'email' => '[email protected]', 'user_logged => TRUE ); $this->session->set_userdata($new_data); Then this is how to call your session data(create a variable and assign it the value of one of the session data you need):

What are sessions in CodeIgniter How do you read write or remove session in CodeIgniter?

In CodeIgniter Session class allows you maintain a user's “state” and track their activity while they are browsing your website. In order to use session, you need to load Session class in your controller.

How check session is set in CodeIgniter?

php $session->set('some_name', 'some_value'); If you want to verify that a session value exists, simply check with isset() : <? php // returns false if the 'some_name' item doesn't exist or is null, // true otherwise: if (isset($_SESSION['some_name'])) { // ... }


1 Answers

Here's the relevant source code of the Session class:

/**
 * Garbage collection
 *
 * This deletes expired session rows from database
 * if the probability percentage is met
 *
 * @access  public
 * @return  void
 */
function _sess_gc()
{
    if ($this->sess_use_database != TRUE)
    {
        return;
    }

    srand(time());
    if ((rand() % 100) < $this->gc_probability)
    {
        $expire = $this->now - $this->sess_expiration;

        $this->CI->db->where("last_activity < {$expire}");
        $this->CI->db->delete($this->sess_table_name);

        log_message('debug', 'Session garbage collection performed.');
    }
}

This function is called in one place, in the constructor of the Session class (almost the last line), so once per request under normal circumstances.

$this->gc_probability is hardcoded to 5 at the top of the class and it doesn't appear to be possible to change it. I'm not sure, but I believe this means that 5% of the time (randomly) the garbage collection will run, clearing out old entries from the sessions DB table.

Note that these old entries are of no significance or harm, the cleanup is only done so your database table does not get overloaded with old, useless records.

like image 190
Wesley Murch Avatar answered Nov 01 '22 06:11

Wesley Murch