EllisLab text mark
Advanced Search
     
Track online users
Posted: 20 March 2008 05:52 AM
Joined: 2008-01-16
8 posts

Hi
I would like to implement track online users (logged in users). In my application session is stored in the database using standard CI’s implementation. Additionaly I created next column in the session table called ‘user_id’ to store logged in user_id’s (after success login).
It works well if user use ‘logout’ feature to log out (then user_id is removed from user_id column) but how to do it working if user close browser or go to another (external) site? If user close browser or go to another site session will be removed from database after $config[‘sess_expiration’] time (7200 seconds by deafult). Is there any way to check it (I would like to avoid javascript)? What if I change $config[‘sess_expiration’] to 600s (session_id is regenerated every 300s)?
I found something intereting here but I don’t know how it works… how to call it?

And one more thing: how to automatically call a function every page load? I’m thinking about clear_online_users() function.

Thanks for help smile

 
Posted: 20 March 2008 09:44 AM   [ # 1 ]   [ Rating: 0 ]
Joined: 2007-10-25
203 posts

Timeouts are your best option. The most simple way would be to create a table containing:
user_id, time_last_action, [optional: last_action]. Each time a user takes and action, you simply update the time_last_action (or whatever name you use) with what action was taken. This allows you to select only the “online” people (who have had activity within the last X amount of time). The amount of time should be determined by your type of website (if it’s mostly a blog, the average user probably isn’t going to spend more than a couple minutes on a page, versus a discussion board where a user may spend 10 minutes writing and revising a post).

clear_online_users() isn’t really a good idea. Making constant calls to delete rows, then a select to get what is left. Much easier to just create the initial activity row when a user first creates an account, then update it over time. Alternatively, if you would prefer to have a “log” of sorts, you could add a new row with activity (giving you a way to track user actions—good for analytics). A bit of warning though—this can result in massive tables, so I would either create an archive table and move rows to is after a certain amount of time, or move them to a log file and write a parser (only if you’re going to log every user action), unless ofcourse you have massive amounts of storage.

To answer your question, you can call a function on each page of a controller from the constructor ( the function ControllerName() ), or in all controllers by extending the Controller and using that extension for your controllers, by using Hooks, or by using Khaos Event Manager.

 
Posted: 20 March 2008 01:14 PM   [ # 2 ]   [ Rating: 0 ]
Avatar
Joined: 2007-06-21
114 posts

Hi PorkOne,

I have a simple buddy system that I use on my website (http://bizwidgets.biz). Right now, it’s set up so you just load the model and get $this->ezbuddy->online_buddies; You could take the code out of the constructor and call the function update_last_click every page load and it only updates your online status every three minutes (or whatever time you choose). It uses my EzAuth model for user data, and you have to add one column to the ez_users table:

last_update datetime

Don’t worry about the $only_friends variable. That’s for implementing a buddy system where only your “friends” on the system show up. Here is the basic code:

<?

//    v 0.1
//    uses ez_users table from ezauth

class EzBuddy_Model extends Model {
    
    
var $online_buddies = array();
    
    function 
EzBuddy_Model() {
        parent
::Model();
        
        
$this->update_last_click();
        
        
$this->online_buddies $this->get_buddies();

    
}
    
    
function get_buddies($params null$only_friends false$only_online true$show_self true{
        
        
if (empty($this->ezauth->user->groups) && $only_friends) return false;
        
        
$this->db->select('distinct ez_users.id, username, email, last_update, register_date, first_name, last_name');
        
        if (
$only_friends{
            $i
=0;
            foreach (
$this->ezauth->user->groups as $id => $grp{
            
                
if ($i==1$this->db->orwhere('group_id'$grp->group_id);
            
                if (
$i==0{ $this->db->where('group_id'$grp->group_id); $i=1}
            }
            
            $this
->db->where('ez_users.id = my_groups.user_id');
            
$this->db->from('my_groups');
        
}
        
        
if (!$show_self && !empty($this->ezauth->user)) $this->db->where('ez_users.id !='$this->ezauth->user->id);
        
        
$this->db->orderby('last_name''asc');
        
$this->db->orderby('first_name''asc');
        
        if (!empty(
$params)) $this->db->where($params);
        
        if (
$only_online$this->db->having('unix_timestamp(last_update) > unix_timestamp() - 300');
        
        
$query $this->db->get('ez_users');
        
        return 
$query->result();
    
}
    
    
function update_last_click() {
        
        
if (empty($this->ezauth->user)) return false;
        
$next_update date("Y-m-d H:i:s"strtotime('-1 minute'));
        
$timestamp date("Y-m-d H:i:s");
        
        
        
//    update database with last click
        
if ($next_update >= $this->ezauth->user->last_update{
            $params 
= array(
                
'last_update'    =>    $timestamp
            
);
            
$this->db->where('id'$this->ezauth->user->id);
            
$this->db->update('ez_users'$params);
            
            
//    update timestamp in session
            
$this->ezauth->user->last_update $timestamp;
            
            
$this->ezauth->update_session($this->ezauth->user);
        
}
    }
    
    
function sign_off() {
        
if (empty($this->ezauth->user)) return false;
        
        
$params = array(
            
'last_update'    =>    null
        
);
        
$this->db->where('id'$this->ezauth->user->id);
        
$this->db->update('ez_users'$params);
    
}
 Signature 

GraphicLeftovers.com - Professional Stock Graphics and Images for Less, powered by CodeIgniter