• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to get _single_ userdata items from session database table

#1
[eluser]czesiek32[/eluser]
Hello, this is my first post Smile

I have my session stored in database, and I want to take from the table some userdata of all users, for example their id's.

It looks like this, there is a stanard db session table:

Code:
CREATE TABLE IF NOT EXISTS  `ci_sessions` (
session_id varchar(40) DEFAULT '0' NOT NULL,
ip_address varchar(16) DEFAULT '0' NOT NULL,
user_agent varchar(120) NOT NULL,
last_activity int(10) unsigned DEFAULT 0 NOT NULL,
user_data text NOT NULL,
PRIMARY KEY (session_id),
KEY `last_activity_idx` (`last_activity`)
);

The userdata field looks like this:

Code:
a:4:{s:7:"user_id";s:1:"2";s:9:"user_name";s:18:"Name Surname";s:10:"user_admin";s:3:"yes";s:19:"user_lastlogin_date";s:10:"2012-02-18";}

And I have a lot of users stored in database. Now i want to get an array of users id's - It is needed to show "who is online" after join with users table in DB.

And here is my question - how could i get this array ? Regexp ? I don't think so. I hope there is a way to get all userdata fields but not looking like s**t (a:4:{s:7:"user_id";s:1:"2";s:9:"user_name";s:18:"Name Surname";s:10:"user_admin";s:3:"yes";s:19:"user_lastlogin_date";s:10:"2012-02-18";}) but like variables ($user_id=2, $user_name="Name Surname") and so on.

I hope you can understand what i mean, thanks for any help.

#2
[eluser]InsiteFX[/eluser]
When ci stores the array in the table it serializes it! So to get the array it needs to be unserizlized.

See ./system/libaries/Session.php

Code:
$user = $this->session->all_userdata();

#3
[eluser]czesiek32[/eluser]
[quote author="InsiteFX" date="1329659687"]When ci stores the array in the table it serializes it! So to get the array it needs to be unserizlized.

See ./system/libaries/Session.php

Code:
$user = $this->session->all_userdata();
[/quote]

Everything is clear for me now, thank you!

#4
[eluser]czesiek32[/eluser]
I have made session model like above - unserialize() is used in constructor, so i have single userdata in class variables Smile

That was my very first time with data serialization, that is why i had no idea what is that before your answer - thanks again.

Code:
<?php

class Session_model extends CI_Model {

var $session_id = '';
var $ip_address = '';
var $user_agent = '';
var $last_activity = '';
var $user_id = '';
var $user_name = '';
var $user_admin = '';
var $user_lastlogin_date = '';

function __construct($session_id = NULL)
{
parent::__construct();
if ($session_id)
{
$this->db->where('session_id',$session_id);
if($query = $this->db->get('session'))
foreach ($query->result() as $row)
{
$this->session_id = $session_id;
$this->ip_address = $row->ip_address;
$this->user_agent = $row->user_agent;
$this->last_activity = $row->last_activity;
$user_data = unserialize($row->user_data);
$this->user_id = $user_data['user_id'];
$this->user_name = $user_data['user_name'];
$this->user_admin = $user_data['user_admin'];
$this->user_lastlogin_date = $user_data['user_lastlogin_date'];
return;
}
}
}

//zwraca tablice id wszystkich sesji zapisanych w bazie danych
//opcjonalnie o czasie aktwynosci w ciagu ostanich $seconds
function getAllSessionsId ($seconds = NULL)
{
$time = time() - $seconds;
$sessions = array();
$this->db->select('session_id');
if ($seconds)
$this->db->where('last_activity >', $time);
$query = $this->db->get('session');
foreach ($query->result() as $row)
{
$sessions[] = $row->session_id;
}
return $sessions;
}


}

#5
[eluser]InsiteFX[/eluser]
You do not need to do it like that, the $this->session->all_userdata() returns all the data to you already unserialized!
Example:
Code:
$data = array();

$data = $this->session->all_userdata();

$this->session_id = $data['session_id'];
$this->ip_address = $data['ip_address'];
$this->user_agent = $data['user_agent'];
$this->last_activity = $data['last_activity'];

#6
[eluser]czesiek32[/eluser]
[quote author="InsiteFX" date="1329742971"]You do not need to do it like that, the $this->session->all_userdata() returns all the data to you already unserialized!
Example:
Code:
$data = array();

$data = $this->session->all_userdata();

$this->session_id = $data['session_id'];
$this->ip_address = $data['ip_address'];
$this->user_agent = $data['user_agent'];
$this->last_activity = $data['last_activity'];
[/quote]

I know that, but in this case i needed to take all (or almost all) users user_data from session table in database, not only mine data which i can retrive by
Code:
$this->session->all_userdata();

#7
[eluser]InsiteFX[/eluser]
Ok, I see what your doing now!


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.