Welcome Guest, Not a member yet? Register   Sign In
Sessions: 'ci_sessions' user_data
#1

[eluser]lorem_ipsum_dolor_sit[/eluser]
Hi all,

I was very happy to see CI had built in session handling and that it would save all the data to a database. I only have one problem, The user_data column of the database is a bit unclear to me. In the database I get:

Code:
a:3:{s:2:"id";s:1:"1";s:8:"username";s:6:"lorem_ipsum";s:9:"logged_in";s:1:"1";}

Is there an easy way to convert this to an array or something similar?

Thanks
#2

[eluser]Dam1an[/eluser]
You don't need to worry about converting this to an array, every time the session class is initiated, it will take that data and de-serialize it into an array, which you then access as normal
#3

[eluser]lorem_ipsum_dolor_sit[/eluser]
I want to run a query on the session data in the database to see who is currently logged in though? Or is there another way of doing that which I am completely missing?
#4

[eluser]Dam1an[/eluser]
Ah, this is something which has been discussed a few times in the forum
Basically you want to track the last_activity of a user which gets updated on every page request for that user. You could either extend the session table to have this or in the users table (or make a seperate table for this). Then when you need to find users currently online, just find the ones which have a recent_activity in the last X minutes.

If you need some more info just search the forums for some similar threads
#5

[eluser]lorem_ipsum_dolor_sit[/eluser]
Right so I did some searching and I came up with this with the help of Phil Sturgeon's suggestion on another thread. I had to modify it because the regular expression he suggested wasn't working..

It is a simple solution, I'm sure there is a better one but I wasn't keen on setting up a table for online users when I already had the information I needed in the database.

Code:
// $user_data should be the user_data column of a row in the session table
// and $user_data_to_retrieve should be the piece of user_data from the session you want
$user_data = $row->user_data;
$user_data_to_retrieve = "username";

// Regular Expression
$regex_logged_in = '/"'.$user_data_to_retrieve.'";s:([0-9]+):"(.*?)";/i';
// Match the data (returns false if nothing matched)
$username = preg_match($regex_username, $user_data, $regs) ? $regs[2] : "FALSE";

Hope that this helps someone..
#6

[eluser]spmckee[/eluser]
Thanks for the tip maxnov but I'm not sure how you are pulling in the $regex_logged_in, $regs, and $regex_username variables.

$regex_logged_in is defined but not used.
$regex_username and $regs are used but not defined.

Any ideas? Thanks!




Theme © iAndrew 2016 - Forum software by © MyBB