CodeIgniter Forums

Full Version: Extending the Session Library - I need to specify database to store sessions in.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]Jaketoolson[/eluser]
I want to store my session activity in the database. I've updated my config file to reflect these changes:
Code:
$config['sess_encrypt_cookie']    = TRUE;
$config['sess_use_database']    = TRUE;
$config['sess_table_name']        = 'users_sessions';
$config['sess_time_to_update']    = 50;

The problem I'm having is my website utilizes more than 1 database, and I need to specify which database the sessions should be stored to. I tried extending the session library but it didn't do a thing:

Code:
class MY_Session extends CI_Session
{
    var $database_name    = 'site_schema';    //database name
    function __construct()
    {
        parent::__construct();
        if ($this->sess_use_database === TRUE AND $this->sess_table_name != '')
        {
            $this->CI->load->database($this->database_name,TRUE);
        }
    }
}

El Forum

[eluser]danmontgomery[/eluser]
Couple of things here...

When you pass TRUE as the 2nd parameter, the DB object gets returned, not assigned to $CI->db. You would need to assign that DB object to something.

Code:
$this->sess_db = $this->CI->load->database($this->database_name, TRUE);

Then, you'd need to do work in pretty much every method in the session class, and replace calls to $this->CI->db with whatever class member you assigned your session db to. sess_read(), sess_write(), etc.

Code:
/**
     * Destroy the current session
     *
     * @access    public
     * @return    void
     */
    function sess_destroy()
    {
        // Kill the session DB row
        if ($this->sess_use_database === TRUE AND isset($this->userdata['session_id']))
        {
            $this->sess_db->where('session_id', $this->userdata['session_id']);
            $this->sess_db->delete($this->sess_table_name);
        }

        // Kill the cookie
        setcookie(
                    $this->sess_cookie_name,
                    addslashes(serialize(array())),
                    ($this->now - 31500000),
                    $this->cookie_path,
                    $this->cookie_domain,
                    0
                );
    }

etc.

El Forum

[eluser]Jaketoolson[/eluser]
Thanks for the help!!
I wish I could just set a variable in config and specify the DB there.