Adding a user id column to CI's session table |
[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
[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)
[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 ![]()
[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'])) { 4. All done! Now you can use this user_id column as you please for any future DB queries ![]()
[eluser]stevefink[/eluser]
Great work, Lone! I'll look into DB Session Lib. :-) Thanks bud, /sf
[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. |
Welcome Guest, Not a member yet? Register Sign In |