CodeIgniter Forums
Adding a user id column to CI's session table - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Adding a user id column to CI's session table (/showthread.php?tid=7712)



Adding a user id column to CI's session table - El Forum - 04-20-2008

[eluser]stevefink[/eluser]
Hi all,

I'm looking to affiliate each row in CI's session table with a particular user. The user has an id in a relating entity (different table). Have any of you found a best practice to incorporate this ID into the session table?

I'm personally thinking just adding a column, stuffing the user_id into the user's session store, then retrieving that and updating the cookie table once it's available. Not sure if I can think of a better algorithm right now.

Any ideas?

/sf


Adding a user id column to CI's session table - El Forum - 04-20-2008

[eluser]adamp1[/eluser]
Why do you need the is on the database? What I would do is either store it in their session cookie, or use a 3rd party session library which allows session variables to be stored in the database. Both these ways means your not changing the way CI works (better for when CI upgrades)


Adding a user id column to CI's session table - El Forum - 04-20-2008

[eluser]Lone[/eluser]
We have used the DB Session library which stores all session info in the DB as opposed to in the cookie. You could then modify it to just add a new user_id column. Infact I think I will add this to our CMS right now cause I can see it being handy down the track Tongue


Adding a user id column to CI's session table - El Forum - 04-20-2008

[eluser]Lone[/eluser]
Ok all done - haven't fully tested but if I find a flaw Ill post it:

1. Install DB Sesssion Library
2. After adding the SQL required for it add the following:

Code:
ALTER TABLE `ci_sessions` ADD `user_id` INT( 11 ) NOT NULL AFTER `session_data` ;

3. Modify Session.php by adding the following to the 'sess_write_database' function (around line 302) with the following:

AFTER
Code:
'last_activity'    => $this->userdata['last_activity']
        );

ADD
Code:
if (isset($this->userdata['user_id'])) {
            $db_data['user_id'] = $this->userdata['user_id'];
        } else {
            $db_data['user_id'] = '';
        }

4. All done! Now you can use this user_id column as you please for any future DB queries Smile


Adding a user id column to CI's session table - El Forum - 04-20-2008

[eluser]stevefink[/eluser]
Great work, Lone! I'll look into DB Session Lib. :-)

Thanks bud,

/sf


Adding a user id column to CI's session table - El Forum - 04-21-2008

[eluser]webthink[/eluser]
any reason why you can't store the data in userdata? $this->session->set_userdata('id',$id); This is the kind of application specific data it's meant for.