Check users online, user last activity, user last ip

You are viewing revision #1 of this wiki article.
This is the latest version of this article.


Stores session data in database and transfer data when session is destroy. Uses for check users online, user's last activity and last ip.


1) Download the component from github and move to protected/components

2) Add this code to component section in config/main.php

'session' => array (
  'class' => 'application.components.DbHttpSession',
  'connectionID' => 'db',
  'sessionTableName' => 'session',
  'userTableName' => 'user'

Session table will be created automatically

Your user table must have needed columns. Add this if you have not:
ALTER TABLE user ADD user_id INT(11) NOT NULL, ADD last_ip VARCHAR(100) NOT NULL, ADD last_activity DATETIME NOT NULL
For Example:

Add this method to User model for check users online

public static function getOnlineUsers()
        $sql = "SELECT session.user_id, FROM session LEFT JOIN user ON";
        $command = Yii::app()->db->createCommand($sql);
        return $command->queryAll();

In view

foreach (User::getOnlineUsers() as $user) {
        echo $user["name"];