[eluser]JoostV[/eluser]
Write a My_Session library in which you add code to save user_ids and fetch them as well.
1. Add column user_id to your session table.
2. On succesful login, delete all sessions that use the current user_id.
Code:
$this->session->delete_by_user_id($user_id);
3. Save user_id to session table.
Code:
$this->session->save_user_id($user_id);
4. Do some extra garbage collection to remove expired sessions
Code:
$this->session->gc_probability = 100;$this->session->sess_gc();
4. In the controller that you whish to show online users, retrieve all users from the session table to display a list of all online users.
Code:
// Fetch online users
$this->data['online_users'] = array();
$this->db->select('users.user_id, users.user_login');
$this->db->from('sessions');
$this->db->join('users', 'sessions.user_id = users.use_id');
$query = $this->db->get();
if ($query->num_rows() > 0) {
$this->data['online_users'] = $query->result_array();
}
5. Display online users in view
Code:
if (count($online_users) > 0) {
foreach ($online_users as $user) {
echo anchor('profile/view/' .$user['user_id'], $user['user_login']) . '; ';
}
}