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

二次信任 提交于 2019-12-23 23:34:47

问题


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


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/7104568/how-do-i-store-database-sessions-in-codeigniter-with-multiple-databases

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!