Skip to content

ci session specific database

Derek Jones edited this page Jul 5, 2012 · 5 revisions

I made some improvements to the session library so you can use a specific database. The reason for this arrangement is that in my development I'm managing multiple databases (more than two), and when you change a driver model, should change the name of the connection in the model, so it does not affect the session CI. It is a simple but effective change. I hope you take advantage.

You must to create a library called MY_Session.php with the following changes:

class MY_Session extends CI_Session{

add two variables: $database_name and oDb. $database_name is the database name and oDb is the object database

    var $CI;
    var $now;
    var $database_name                 = '';    //database name
    var $oDb ;

In the constructor MY_Session change the following lines:

    // Are we using a database?  If so, load it
    if ($this->sess_use_database === TRUE AND $this->sess_table_name != '')
    {
        $this->CI->load->database();
    }

by

    // Are we using a database?  If so, load it
    if ($this->sess_use_database === TRUE AND $this->sess_table_name != '')
    {
        $this->oDb = $this->CI->load->database($this->database_name,TRUE);
    }

Then change all the lines when are making database connection whith $this->oDb:

$this->CI->db->where('session_id', $session['session_id']);

by

$this->oDb->where('session_id', $session['session_id']);

To specify the database, add the following line in the file config.php:

$config['database_name']         = 'database_name';

This specify the group name from the file database.php

The library and functions works as usual. Also, you can disable de database, if you want.

The file my_session.php is attached.

File:MY_Session.zip

Clone this wiki locally