Welcome Guest, Not a member yet? Register   Sign In
Sessions in database - How to retrieve/deserialize user_data items
#1

[eluser]mradlmaier[/eluser]
Hello all,

I have my sessions stored in the database.

So the user_data itimes are stored in one long String, which outputted with var_dump() looks like this:

Quote:string(86) "a:3:{s:11:"logged_user";s:5:"michi";s:7:"user_id";s:1:"9";s:8:"username";s:5:"michi";}"

Does anybody know an easy way to read the value of user_id and username?

Somewhere in CI there must already exist a function whih does the deserializing of this string.
Just dont know which and how to call that function?

Thanks,
Michael
#2

[eluser]brianw1975[/eluser]
You could try using a regexp to get what you want. MySQL has support for it in the query... but I'm not sure how bad the impact will be on the query processing time.
#3

[eluser]pistolPete[/eluser]
Why don't you use
Code:
$user_id = $this->session->userdata('user_id');
$username = $this->session->userdata('username');
?

User guide: http://ellislab.com/codeigniter/user-gui...sions.html

There is also a native PHP function called unserialize(): http://php.net/unserialize
#4

[eluser]mradlmaier[/eluser]
Because like this you only get the current user´s session data, not the sessions from all user. You see, the scope is just the current user, and i need all sessions.
#5

[eluser]mradlmaier[/eluser]
Maybe the native PHP function will do the job, though. Just was hoping to reuse CI system code, because that would be more elegant...
#6

[eluser]jedd[/eluser]
[quote author="mradlmaier" date="1253413957"]
Maybe the native PHP function will do the job, though. Just was hoping to reuse CI system code, because that would be more elegant...
[/quote]

I don't imagine there's any CI code to reuse here, as the CI session library isn't intended as an ersatz user table. If you want to record information about your users, use a real user table.

You could, as PistolPete intimated, just run through the contents of your ci_session table and unserialize() the user_data column. Identifying that back to a given user might be challenging, as the ci_session table doesn't rely on your application's understanding of user id's. Presumably you just keep that somewhere in the user_data array - though it's getting messier, huh. Sure you want to do it this way rather than doing it properly? Wink
#7

[eluser]mradlmaier[/eluser]
i dont intend to use session as my "ersatz"/"replacement" user table. I just want a list of currently active sessions. In my book, this is not a very special use case, many sites offer the feature to see currently active users.

The CI Session class somehow reads and writes this data, so why shouldn´t i elegantly try to reuse existing CI code?

In my humble opinion, that would be smart to do. Dont duplicate the same functionality in your code, that is bad programming practise.

But if nobody has an idea where to hook into, i will probably go the native PHP way...
#8

[eluser]jedd[/eluser]
Ahh, okay.

How about your user table, where (last login time > last logout time) ?

I believe sessions don't expire (at least not fast (though you can change that setting of course)), but in any case you're always going to have a certain fuzziness with the calculation of 'show me currently logged in users'.
#9

[eluser]InsiteFX[/eluser]
My old Auth system does that, users are assigned a guest level until they login.

user table
roles table
active_guests table
active_users table
banned_users table
etc.

if a guest login they are move to the active_user table and deleted out of the active_guests table.

Enjoy
InsiteFX
#10

[eluser]brianw1975[/eluser]
oh c'mon, I cannot be the only one who doesn't want to use a back-hoe to plant a flat of mums...

all the data needed is there:

Quote:string(86) “a:3:{s:11:“logged_user”;s:5:“michi”;s:7:“user_id”;s:1:“9”;s:8:“username”;s:5:“michi”;}”

serialized data can be read without decoding...

logged_user = "michi"
user_id = "9"
username = "michi"

get all of the currently active sessions and either deserialize it or regex the needed information out...

Code:
$query = "select * from {session data table} where last_activity > {5 minutes ago?}";
foreach($query->row as $u){
$info = deserialize($u);
if($info['user_id'] != {guest account id number})
   $logged_in_count++;
else
   $guest_count++;

$active_users[] = $info['username'];
}
$active_users = explode(', ',$active_users);

of course this is just off the top of my head and completely untested code, not to mention, not the only way to do the query either.




Theme © iAndrew 2016 - Forum software by © MyBB