Yii 1.1: Check users online, user last activity, user last ip

7 followers

DbHttpSession

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

Instructions ΒΆ

1) Download the component from github https://github.com/frazelli/DbHttpSession 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, user.name FROM session LEFT JOIN user ON user.id=session.user_id";
        $command = Yii::app()->db->createCommand($sql);
 
        return $command->queryAll();
    }

In view

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

Total 5 comments

#18781 report it
hshfcb at 2015/01/06 02:17am
Nice job

Thank Yii <3

#18773 report it
Victor Lacerda Azevedo at 2015/01/04 05:57pm
Nice job

Like a charm.

#18178 report it
Rohit Suthar at 2014/09/22 06:55am
Nice

Nice work man :)..

#18175 report it
Rajith R at 2014/09/22 05:51am
Good work

Good work , use with yii user module .

#18159 report it
VivekYii at 2014/09/18 10:25am
great tutorial

Thank yii

Leave a comment

Please to leave your comment.

Write new article