Roopa
Roopa

Reputation: 159

How do I store database sessions in CodeIgniter with multiple databases?

I am using codeigniter and till date I was using cookie based sessions.. but now size of sessions is increased so i need to store them in database..

I have multiple database(more than 3) connected to my application..How can i specifically use ci_sessions table to store sessions?

when i say $config['sess_use_database'] = TRUE; my app goes blank. a plane white page is shown.

That is because From multiple database connections codeigniter is not finding exact database to store sessions..

How can i achieve the same? Thanks

Upvotes: 2

Views: 2953

Answers (2)

VictorKilo
VictorKilo

Reputation: 1870

It has been a while since this question has been asked, but I feel like a conclusive answer is needed.

As of CodeIgniter 2.1.2

When the system/libraries/Session.php is loaded, it also loads a database to use with the sessions, if specified.

The default code used is (Line 85):

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

This should, in theory load the default database. For some reason, this did not work for me. I had to modify the code to specifically call the default database.

The code you should use to pull from the default database:

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

If you would like to use a different database, just change default to the name of the database which you would like to use.

Note: If you load another database before you call session data, you will need to specify your session data again. The session will continue to try and pull from the currently active database.

Upvotes: 3

Sukumar
Sukumar

Reputation: 3587

Looking at the session code from system/libraries/Session.php, one can see that it uses the default $this->db to connect to the database for storing sessions.

Try modifying your database groups such that $this->db works. Alternatively, you can extend the Session.php such that you can pass it the database group for storing sessions.

Hope this gets you started in the right direction.

Upvotes: 2

Related Questions