yii2-cookbook icon indicating copy to clipboard operation
yii2-cookbook copied to clipboard

Logging out specified user, listing who's online by using DB sessions

Open samdark opened this issue 9 years ago • 5 comments

See https://github.com/yiisoft/yii2/issues/10404.

Could be solved by using DB sessions and writing additional info into the table:

'session' => [
    'class' => 'yii\web\DbSession',
    'writeCallback' => function ($session) {
        return [
           'user_id' => Yii::$app->user->id,
           'last_write' => time(),
        ];
    },
],

Then it's easy to work with it via SQL:

-- Kill user sessions. You need to invalidate user cookie as well (change)
DELETE FROM session WHERE user_id = :user_id;
-- Need to change auth key in user table afterwards else "remember me" cookie won't be invalidated.

-- Get all users active in recent hour
SELECT user_id FROM session WHERE last_write - 3600 < :now;

samdark avatar Dec 16 '15 12:12 samdark

http://stackoverflow.com/questions/34207606/yii2-logout-spesific-user/34207850#34207850

SilverFire avatar Dec 16 '15 12:12 SilverFire

http://yiiframework.ru/forum/viewtopic.php?f=4&t=7486

samdark avatar Dec 16 '15 18:12 samdark

In my case It began from https://github.com/yiisoft/yii2/issues/9718

ElisDN avatar Dec 16 '15 19:12 ElisDN

what about the same functional with yii\redis\Session? How I can count authorized users?

Senegal avatar Mar 31 '17 03:03 Senegal

@Senegal to some like this https://github.com/bscheshirwork/yii2-redis-session

bscheshirwork avatar Aug 23 '17 12:08 bscheshirwork