samdark / yii2-cookbook

Yii 2.0 Community Cookbook
1.44k stars 297 forks source link

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

Open samdark opened 8 years ago

samdark commented 8 years ago

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;
SilverFire commented 8 years ago

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

samdark commented 8 years ago

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

ElisDN commented 8 years ago

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

Senegal commented 7 years ago

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

bscheshirwork commented 6 years ago

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