简体   繁体   English

如何在具有多个数据库的CodeIgniter中存储数据库会话?

[英]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.. 我正在使用codeigniter,直到到目前为止,我正在使用基于cookie的会话..但是现在会话的大小增加了,所以我需要将它们存储在数据库中。

I have multiple database(more than 3) connected to my application..How can i specifically use ci_sessions table to store sessions? 我有多个数据库(超过3个)连接到我的应用程序。我如何专门使用ci_sessions表存储会话?

when i say $config['sess_use_database'] = TRUE; 当我说$ 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.. 这是因为codeigniter从多个数据库连接中找不到要存储会话的确切数据库。

How can i achieve the same? 我怎样才能达到同样的目的? Thanks 谢谢

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 从CodeIgniter 2.1.2开始

When the system/libraries/Session.php is loaded, it also loads a database to use with the sessions, if specified. 加载system/libraries/Session.php ,如果已指定,它还将加载数据库以供会话使用。

The default code used is (Line 85): 使用的默认代码是(第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: 您应使用从default数据库中提取的代码:

// 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. 如果要使用其他数据库,只需将default更改为要使用的数据库名称。

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. 该会话将继续尝试从当前活动的数据库中提取数据。

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. 查看system/libraries/Session.php中的会话代码,可以看到它使用默认的$this->db连接到数据库以存储会话。

Try modifying your database groups such that $this->db works. 尝试修改数据库组,以使$this->db有效。 Alternatively, you can extend the Session.php such that you can pass it the database group for storing sessions. 另外,您可以扩展Session.php ,以便可以将数据库传递给它以存储会话。

Hope this gets you started in the right direction. 希望这可以帮助您朝正确的方向开始。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM