duanjia7607 2016-01-06 08:50
浏览 23
已采纳

在线用户计数器Yii2

I want to count all online users in my Yii 2 application, what is your solution?

even I want to count authenticated users. one solution is to over ride authenticate method in login and after login I set a record in db. but here I have a problem. How should I determine when a user is offline? If I change online flag in logout action it has some problem. maybe some users close the browser without login. then database will shows him online. what is best solution for this problem?

Edit

My question is how to over ride session expire method. Not change expiration time. In more details, I want determine if a user is offline even he closes the browser without logout action

  • 写回答

2条回答 默认 最新

  • dsm0688 2016-01-06 11:10
    关注

    You could store sessions in database and the framework will handle expiration for you.

    This example is from advanced template and I've takes session from frontend users.

    I needed to create and use a custom component to store also the user id.

    1) Create a table on db:

        CREATE TABLE `session_frontend_user` (
        `id` char(80) CHARACTER SET utf8 NOT NULL,
        `user_id` int(11) DEFAULT NULL,
        `ip` varchar(15) CHARACTER SET utf8 NOT NULL,
        `expire` int(11) DEFAULT NULL,
        `data` longblob,
        PRIMARY KEY (`id`),
        KEY `expire` (`expire`)
        ) ENGINE=InnoDB
    

    2) Create a model from this table:

    <?php
    
    namespace common\models;
    
    use Yii;
    
    /**
     * This is the model class for table "session_frontend_user".
     *
     * @property string $id
     * @property integer $user_id
     * @property string $ip
     * @property integer $expire
     * @property resource $data
     */
    class SessionFrontendUser extends \yii\db\ActiveRecord
    {
        /**
         * @inheritdoc
         */
        public static function tableName()
        {
            return 'session_frontend_user';
        }
    
        /**
         * @inheritdoc
         */
        public function rules()
        {
            return [
                [['id', 'ip'], 'required'],
                [['user_id', 'expire'], 'integer'],
                [['data'], 'string'],
                [['id'], 'string', 'max' => 80],
                [['ip'], 'string', 'max' => 15]
            ];
        }
    
        /**
         * @inheritdoc
         */
        public function attributeLabels()
        {
            return [
                'id' => 'ID',
                'user_id' => 'User ID',
                'ip' => 'Ip',
                'expire' => 'Expire',
                'data' => 'Data',
            ];
        }
    }
    

    3) Change configuration in config/main.php

    'components' => [
    
        'session' => [
            'class' => '\frontend\components\CustomDbSession',
            // 'db' => 'mydb',  // the application component ID of the DB connection. Defaults to 'db'.
            'sessionTable' => 'session_frontend_user', // session table name. Defaults to 'session'.
        ], 
    

    4) Create the custom component 'CustomDbSession' required in configuration in path \frontend\components:

    <?php
    
    namespace frontend\components;
    
    
    use Yii;
    use yii\db\Connection;
    use yii\db\Query;
    use yii\base\InvalidConfigException;
    use yii\di\Instance;
    
    class CustomDbSession extends \yii\web\DbSession {
    
        public $writeCallback = ['\frontend\components\CustomDbSession', 'writeCustomFields'];
    
        public function writeCustomFields($session) {
    
            try
            {
                $uid = (\Yii::$app->user->getIdentity(false) == null)?null:\Yii::$app->user->getIdentity(false)->id;
                return [ 'user_id' => $uid, 'ip' => $_SERVER['REMOTE_ADDR'] ];
            }
            catch(Exception $excp)
            {
                \Yii::info(print_r($excp), 'informazioni');
    
    
            }
        }
    
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 有人会SIRIUS 5.8.0这个软件吗
  • ¥30 comsol仿真等离激元
  • ¥15 静电纺丝煅烧后如何得到柔性纤维
  • ¥15 (标签-react native|关键词-镜像源)
  • ¥100 照片生成3D人脸视频
  • ¥15 伪装视频时长问题修改MP4的时长问题,
  • ¥15 JETSON NANO
  • ¥15 VS开发qt时如何在paintgl函数中用pushbutton控制切换纹理
  • ¥20 关于 openpyxl 处理excel文件地问题
  • ¥15 MS中不知道高分子的构型怎么构建模型