如何使用DbSession在Yii2中创建用户会话管理系统 [英] How to create user session manage system in Yii2 with DbSession

查看:186
本文介绍了如何使用DbSession在Yii2中创建用户会话管理系统的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天,当我想为我的网站创建用户个人资料页面并想要创建系统时,用户可以在此系统需求中管理他的活动会话

  • 查看活动会话的浏览器和平台
  • 查看当前会话是什么
  • 删除不需要的活动会话

我们如何做到这一点?

解决方案

我找到了将用户user_id保存到会话表中的解决方案

Yii2数据库会话-存储其他属性和用户信息

注销指定用户,使用数据库会话列出谁在线

但是实现此问题还有另一个问题:如何根据用户会话的浏览器和平台对其进行分类,我找到了此解决方案.

我使用此类和下面的代码在会话数据库中添加了另一列,并在其中我保存了浏览器信息和用户平台信息.

'components' => [
    //...
    'session' => [
                'class' => 'yii\web\DbSession',
                'writeCallback' => function ($session) {
                    $user_browser = null;
                    if (!Yii::$app->user->isGuest) {
                        $browser = new \BrowserDetection();
                        $user_browser = "{$browser->getName()}-{$browser->getPlatform()}" . ($browser->is64bitPlatform() ? "(x64)" : "(x86)") . ($browser->isMobile() ? "-Mobile" : "-Desktop");
                    }
                    return [
                        'user_id' => Yii::$app->user->id,
                        'last_write' => new \yii\db\Expression('NOW()'),
                        'browser_platform' => $user_browser
                    ];
                }
            ],
   // ...
]

注意::您将其他数据添加到会话表中

但是要找到当前会话,我在检查Yii::$app->session->id并返回空字符串值时遇到了问题,我在下面的链接中找到了解决方案.

模块的BeforeAction中的空白会话ID

在Yii2-Advanced中,

我创建frontend\components\baseController并从中扩展所有控制器,并创建beforeAction()并在其中打开会话,如下所示:

namespace frontend\components;
class BaseController extends \yii\web\Controller
{
    public function beforeAction($action)
        {
            if (parent::beforeAction($action)) 
            {
                Yii::$app->session->open();
                return true;
            }
              return false;
    }
}

最后,我为会话数据库创建ActiveRecord模型,并在配置文件页面中使用它来显示或删除用户会话

Today when I want to create user profile page for my website and want to create system users can manage his active sessions in this system need

  • View active sessions Browsers and platform
  • See what is current session
  • remove unwanted active sessions

How we can do this?

解决方案

I Find this solutions to save my users user_id into session table

Yii2 database session - store additional attributes and user information

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

But there is another problem in implementing this issue: how to classify user sessions based on their browser and platform, and I found this solution.

I added another column to the session database, and inside it, using this class and the code below I saved the browser information and user platform information.

'components' => [
    //...
    'session' => [
                'class' => 'yii\web\DbSession',
                'writeCallback' => function ($session) {
                    $user_browser = null;
                    if (!Yii::$app->user->isGuest) {
                        $browser = new \BrowserDetection();
                        $user_browser = "{$browser->getName()}-{$browser->getPlatform()}" . ($browser->is64bitPlatform() ? "(x64)" : "(x86)") . ($browser->isMobile() ? "-Mobile" : "-Desktop");
                    }
                    return [
                        'user_id' => Yii::$app->user->id,
                        'last_write' => new \yii\db\Expression('NOW()'),
                        'browser_platform' => $user_browser
                    ];
                }
            ],
   // ...
]

note : you add other data to your session table

But to find the current session, I encountered a problem when I checked Yii::$app->session->id and returned the empty string value and I found the solution in the link below.

Blank Session ID in Module's BeforeAction

in Yii2-Advanced I create frontend\components\baseController and extend all controllers from it and create beforeAction() and open session in it something like this:

namespace frontend\components;
class BaseController extends \yii\web\Controller
{
    public function beforeAction($action)
        {
            if (parent::beforeAction($action)) 
            {
                Yii::$app->session->open();
                return true;
            }
              return false;
    }
}

At the end I create ActiveRecord model for session database and use it in profile page to show or remove user sessions

这篇关于如何使用DbSession在Yii2中创建用户会话管理系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆