• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Extending the Session Library - I need to specify database to store sessions in.

I want to store my session activity in the database. I've updated my config file to reflect these changes:
$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:

class MY_Session extends CI_Session
    var $database_name    = 'site_schema';    //database name
    function __construct()
        if ($this->sess_use_database === TRUE AND $this->sess_table_name != '')

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.

$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.

     * 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']);

        // Kill the cookie
                    ($this->now - 31500000),


Thanks for the help!!
I wish I could just set a variable in config and specify the DB there.

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.