Welcome Guest, Not a member yet? Register   Sign In
check who is online using ci_sessions
#1

[eluser]humugus[/eluser]
hi Smile
i tried to find an answer by digging in the forum but i could not, excuse me in advance of my question has been discussed elsewhere and i missed it.
what i try to do is :
build a list of users, with a sign on those who are currently online
i use db for sessions and DX_Auth for validation
a typical session record as constructed by CI and DX_Auth is :
---------
user data (BLOB):
a:9:{s:10:"DX_user_id";s:1:"3";s:11:"DX_username";s:7:"humugus";s:10:"DX_role_id";s:1:"2";s:12:"DX_role_name";s:5:"Admin";s:18:"DX_parent_roles_id";a:0:{}s:20:"DX_parent_roles_name";a:0:{}s:13:"DX_permission";a:0:{}s:21:"DX_parent_permissions";a:0:{}s:12:"DX_logged_in";s:1:"1";}

last activity (BIGINT): 1235513662
---------
for me it is obvious that all data i need are already stored (last activity and user_id)
but i could not so far get info for the rest of the users but myself form the database, therefore i do not know if anyone else is online or not

any idea on how i could get the session info (if exist) for any user id ??
#2

[eluser]Relexx[/eluser]
how are you trying to get the info? $this->session->userdata('') or parsing the userdata field in the db. It may be just as easy to store the current users, than try and parse the ci_session userdata field.
#3

[eluser]humugus[/eluser]
that is my question actually :
"how do i get the session data for a given user ?"

i know i could create a table and store new data there, but since session table has the same data as well i thought there might be a way to get that data and avoid double records, and the overhead to the database
using similar to this approach :

$this->obj->session->userdata('logged_in')

always points to the user who triggered the code

i need something like (pseudo code):
foreach (user in users table) :
check in sessions table to see if he is online <--- i cannot achieve this
if he is online
put him on the users list web page with green fonts
else
put him on the users list web page with red fonts
endforeach;
#4

[eluser]TheFuzzy0ne[/eluser]
You'll need a library that extends the session library, that effectively logs the activity of your logged in users to the database.
#5

[eluser]Phil Sturgeon[/eluser]
Or grab all the session data and perform a regular expression on the result. That way is not quite so pretty, but is the only way you can avoid duplicating the session data for who is on and offline.

The expression would just be something like:

Quote:/“DX_username”;s\:[0-9]\:“([0-9a-z_])”/i

Then return an array of anyone who matches the rule. Could even jam the regex into the query if you like that sort of thing.
#6

[eluser]humugus[/eluser]
thank you all for your replies Big Grin
the closest i could go was a query like this one :
-----------
$username = the unique user's name

SELECT `last_activity`
FROM `ci_sessions`
WHERE `user_data` LIKE "%' . $username . '%"
-----------------
the above returns a recordset of 1 record if user is online, or 0 if he is offline which is good for what i need

hopefully it is useful to someone else, or someone could come with a better approach
#7

[eluser]humugus[/eluser]
a small function added on a helper :

Code:
function offline($user_id)
{
    $CI =& get_instance();
    $sql_string =
'SELECT last_activity FROM ci_sessions WHERE user_data LIKE "%'.get_field('users','username', $user_id).'%"';    
    $query = $CI->db->query($sql_string);
    if ($query->num_rows() > 0) {return FALSE;}
    return TRUE;
}

function get_field($table, $field, $id)
{
    $CI =& get_instance();
    $CI->db->where('id', $id);
    $query = $CI->db->get($table);
    $row = $query->row();
    return $row->$field;
}




Theme © iAndrew 2016 - Forum software by © MyBB