Skip to content

Library: Online Users

Michael Förster edited this page May 6, 2013 · 9 revisions

Theory of operation

Every user are identified by the IP, so there will be no repeated data. On every hit, we update the last hit time to keep the user active. 

There is a "time_limit" that is used to know if the user is not active anymore. All the users that are not active anymore are deleted from the record file. We know how many users are online counting the records on the file.

Class basic features

Auto-update hit times
Auto-clean expired hits
Easy-way to display how many users are online
Database server NOT needed (it uses text file instead)

Class advanced features

You can use custom data for the user; it's useful if you want to show off which members are logged in the system
Last page visited (returned by $_SERVER['REQUEST_URI']) is recorded
Robots are automatically detected using the USER_AGENT library

Installation

Download the class (File:OnlineUsers0.2.zip) and uncompress it to your APPLICATION/LIBRARIES directory

Change line #16 with your custom database file (requires read/write permissions)

Change line #26 and set the number of seconds for the user not being considered online anymore. (timeout)

Not tested on PHP 4

Using the class

Loading the class

You can load this class as the same way you do to load another CI's classes
$this->load->library('OnlineUsers');
... and it will be accessible trough
$this->onlineusers

Note: If the user is a 'new' one, we load the USER_AGENT library if it's not loaded yet and destroy it after (to prevent bugs if you try to load it again). So, if you want to use the USER_AGENT library for other things, you should load it BEFORE the loading of the OnlineUsers class so we'll need only one instance of it and get best performance.

Showing the total number of online users

$this->onlineusers->total_users();
Returns the number of users that are active
Ex: <?=$this->onlineusers->total_users();?>

Showing the number of online robots

$this->onlineusers->total_robots();
Returns the number of robots that are visiting your site. The robots are discovered using the USER_AGENT class
Tip: You can get the number of nom-robots visitors by subtracting the total_robots() value from total_users() value

Setting custom data

$this->onlineusers->set_data(array('key'=>$value));
You can record the username of a logged user (for example) to show it in another page.

To optimize speed, the data will be set only once, but if you add a new data, it will work. This example will not work:

$userdata['username'] = 'Peter';
$userdata['id'] = 5;
$this->onlineusers->set_data($userdata);

$usrdata['username'] = 'John';
$userdata['id'] = 3;
$this->onlineusers->set_data('John'); //Doesn't work. The user's username is still Peter and id=5

The example before will not work, but you can force the change setting the second parameter to true:

$userdata['username'] = 'Peter';
$userdata['id'] = 5;
$this->onlineusers->set_data($userdata);

$usrdata['username'] = 'John';
$userdata['id'] = 3;
$this->onlineusers->set_data($usrdata);  //Works. The user's username is now John and id=3

Note that you should prevent using it. Every user_id changes requires a new writing to the database file. I suggest you to use the second parameter only after an user authentication.

<strong>This is not a session class. Do not record so much data.</strong>

This optimization was implemented because the user's session can be alive for longer than the TIMEOUT. For example, you have defined a session time to 1 hour and the timeout of this class to 15min, if the user returns after 30 minutes, he/she will still be logged, but not shown in the members online list. So, to prevent this, don't be afraid to use the set_id() function on every controller you have.

Ex:


class Blog extends Controller {
    
    function Blog()    {

        parent::Controller(); 
        $this->load->library('session');
        $this->load->library('UsersOnline');
        $userdata['username'] = $this->session->username;
        $userdata['id']=$this->session->user_id;
        $this->onlineusers->set_data($userdata);

    }

    function index(){}
}

Advanced users listing

$this->onlineusers->get_info();
This function returns a multi-dimensional array with all the information recorded by the library. The user's ip, the last page visited, if its a robot and the custom data you had set using the set_data() function. You have to write your own routine to work with it. Here is the structure of this array:

$user['xxx.xxx.xxx.xxx']
    ['time'] => the last hit timestamp
    ['uri'] => the last page visited (got with $_SERVER['REQUEST_URI'])
    ['bot'] => The robot's name or false if the user is not recognized as a robot
    ['data'] => an array of the custom data you have added (set by set_data())

Simple example on how to list the online members

$users = $this->onlineusers->get_info(); //prefer using reference to best memory usage
foreach($users as $user) 
{
    if(isset($user['data']['username'])) { print $user['data']['username']; } 
}
Clone this wiki locally