Welcome Guest, Not a member yet? Register   Sign In
How -not- to use the default database for the session-data
#1

[eluser]Unknown[/eluser]
Dear CI-users,

I developed two applications, each with a different database and I would like them to share the same session-data. So I would like to change the second application to use the session-data table of the first application. But I cannot find any setting to change the default database (just for the sessions)...

<?php
$config['sess_cookie_name'] = 'ci_session';
$config['sess_expiration'] = 7200;
$config['sess_encrypt_cookie'] = TRUE;
$config['sess_use_database'] = TRUE;
$config['sess_table_name'] = 'sessies';
$config['sess_match_ip'] = FALSE;
$config['sess_match_useragent'] = TRUE;
$config['sess_time_to_update'] = 300;
?>

Am I missing something? Could someone guide me towards sharing the same session data?
#2

[eluser]aidehua[/eluser]
One way to achieve this would be to over-ride the default CodeIgniter session library.

Create a MY_session.php file and save it in /application/libraries. Copy and paste the default CI Session library (/system/libraries/Session.php). You really only need to change one line, which is line 80:

Code:
$this->CI->load->database();

This line tells the Session class which database to connect to. As it stands it will connect to the default database as defined in your config file.

Change this line as set out in the section called "Manually Connecting to a Database" on
this page in the user guide.

That should do the trick, I think.
#3

[eluser]Unknown[/eluser]
Creating a MY_Session.php file, copying the /system/libaries/Session.php file and editing in line 80 and changing the classname to MY_Session results does not work. Nothing happends, no errors, no new sessions in the database... nothing.

To be honest, even accessing the seconds database, using the manual, is something I could not get to work...

<?php
$DB1 = $this->load->database('group_one', TRUE);
$DB2 = $this->load->database('group_two', TRUE);
?>
#4

[eluser]Unknown[/eluser]
[quote author="aidehua" date="1263231523"]One way to achieve this would be to over-ride the default CodeIgniter session library.

Create a MY_session.php file and save it in /application/libraries. Copy and paste the default CI Session library (/system/libraries/Session.php). You really only need to change one line, which is line 80:

Code:
$this->CI->load->database();

This line tells the Session class which database to connect to. As it stands it will connect to the default database as defined in your config file.

Change this line as set out in the section called "Manually Connecting to a Database" on
this page in the user guide.

That should do the trick, I think.[/quote]


aidehua,

To make this work I created a ci_session table in the database I wanted to use. Created a database group in the database config file, pointing to the database. Copied Session.php from the CI library to my application library and made the below changes to the copy.

:Edit application/config/database.php:
Add a database group to the database.php config file, which points to the
database you want to store your session info in. Name of group is session.
Code:
$db['session']['hostname'] = "<hostname>";
$db['session']['username'] = "<user>";
$db['session']['password'] = "<password>";
$db['session']['database'] = "<database containing session table>";
$db['session']['dbdriver'] = "mysql";
$db['session']['dbprefix'] = "";
$db['session']['pconnect'] = TRUE;
$db['session']['db_debug'] = TRUE;
$db['session']['cache_on'] = FALSE;
$db['session']['cachedir'] = "";
$db['session']['char_set'] = "utf8";
$db['session']['dbcollat'] = "utf8_general_ci";

:Edit application/libraries/Session.php:
Added class variable:
Code:
var $DB1;

Line 80 is changed to:
Code:
$this->DB1 = $this->CI->load->database('session',TRUE);

Then any database calls starting with:
Code:
$this->CI->db...
Example:
Code:
$this->CI->db->where(...);

are changed to:
Code:
$this->DB1
Example:
Code:
$this->DB1->where(...);




Theme © iAndrew 2016 - Forum software by © MyBB