如何使用CodeIgniter在系统中显示实时在线用户? [英] How to display the live online user in the system using CodeIgniter?

查看:76
本文介绍了如何使用CodeIgniter在系统中显示实时在线用户?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用CodeIgniter,正在显示使用CodeIgniter的在线实时用户.

I am using CodeIgniter, I am showing live online user using CodeIgniter.

我尝试了一些代码,但是现在我很困惑.

I tried some code but I am confused now.

参考视频.我从这段视频中学到了

Reference video. I learn from this video

https ://www.webslesson.info/2017/08/display-online-users-using-php-mysql-with-ajax-jquery.html

OR

步骤:1 https://www.youtube.com/watch?v=aAG3w8l8lL8

步骤:2 https://www.youtube.com/watch?v=L__8vVJT57U

这是我的代码.

登录密码验证

如果密码正确,它将在数据库中插入时间和用户ID.

If the password is correct then It will insert the time and user id in the database.

    if ($result) {             
    if(password_verify($password,$result->password))
        {
            $data_login= array('emp_id' =>$result->id ,'last_activity' =>date("Y-m-d H:i:s", STRTOTIME(date('h:i:sa'))));
            $this->db->insert('tbl_current_login',$data_login);  
            $last_id=$this->db->insert_id();
            if (!empty($last_id)) {
            $active_user_session = array('id' => $result->id,'access_role'=>$result->access_role);
            $this->session->set_userdata('active_user_session',$active_user_session);
            return $result;

                }

                else 
                {
                    return false;
                }       
    }

    else{
        return false;
    }
}

现在,我在索引页面上更新了脚本.

Now I updated the script on the index page.

$(document).ready(function(){
   <?php 
    if ($this->session->userdata['active_user_session']['id']) {?>
    function update_user_activity(){
       var active_time='update_time';
           $.ajax({
              url:"<?php echo base_url(); ?>/Employee_control/update_login_time",
                method:"POST",
                data:{active_time:active_time},
                success:function(data){
                 //alert(data);
                   }
                   });
                 }
              setInterval(function(){
               update_user_activity()
                 },3000);
              <?php
               }?>
             });

员工控制者

public function update_login_time(){
     $active_time=trim($this->input->post('active_time'));
     if (isset($active_time)) {
          $this->Employee_model->update_last_login();
     }
}

员工模型

public function update_last_login(){
      $data = array(
                   'last_activity' =>date("Y-m-d H:i:s", STRTOTIME(date('h:i:sa'))),
                   'emp_id' =>$this->session->userdata['active_user_session']['id']
                );

    $this->db->where('emp_id', $this->session->userdata['active_user_session']['id']);
    $this->db->update('tbl_current_login', $data); 
    }

现在我的问题是, 如果用户第一次登录,则登录时间将插入表中;如果用户第二次登录,则将再次插入新的登录时间.正确的?.更新存在一些问题.它正在更新与id相关的所有last_activity时间.如果是这样,那么到底如何才能确定用户登录的总小时数?以及如果更新所有last_activity时间,我们如何检查用户的历史记录?

Now My issue is, if user login first time then login time will insert in the table and if user login second time then it will insert again new login time. Right?. There is some issue with an update. It's updating all the last_activity time which is related to the id. If this is true then at the end of the day how can we identify the total number of hours user logged in? and how can we check the history of the user if we update the all the last_activity time?

我只想在系统中显示实时在线用户. 您能帮我解决这个问题吗?

I just want to display the live online user in the system. Would you help me out with this issue?

推荐答案

您可以在登录验证方法中使用以下代码设置active_user_session会话数据

You set the active_user_session session data using the below code in your login verify method

$active_user_session = array('id' => $result->id,'access_role'=>$result->access_role);
$this->session->set_userdata('active_user_session',$active_user_session);

如果用户ID为8,则上面的$result->id的值将是8,这也意味着下面的模型代码中的$this->session->userdata['active_user_session']['id']的值将是8

If the user id is 8, then the value of $result->id above will be 8 and that also means $this->session->userdata['active_user_session']['id'] in your model code below will be 8

public function update_last_login(){
    $data = array(
               'last_activity' =>date("Y-m-d H:i:s", STRTOTIME(date('h:i:sa'))),
               'emp_id' =>$this->session->userdata['active_user_session']['id']
            );

    $this->db->where('emp_id', $this->session->userdata['active_user_session']['id']);
    $this->db->update('tbl_current_login', $data); 
}

假设当前时间为2018-09-16 08:59:16,则您上面的模型代码会生成并执行以下查询

Assuming current time is 2018-09-16 08:59:16, your model code above generates and executes the following query

UPDATE tbl_current_login
SET last_activity = '2018-09-16 08:59:16', emp_id = 8
WHERE emp_id = 8

但是这是错误的查询,因为每次用户登录时都会添加每个tbl_current_login记录,而您真正想要更新的只是该特定用户的最新记录.这是您要查询的查询

However it's the wrong query because each tbl_current_login record is added every time a user logs in and what you really want to update is only the latest record for that particular user. This is the query you're looking for

UPDATE tbl_current_login
SET last_activity = '2018-09-16 08:59:16'
WHERE login_id = 5

要获取正确的查询,请在您的登录验证方法中更改此部分代码

To get the correct query, change this portion of code in your login verify method

$data_login = array('emp_id' =>$result->id ,'last_activity' =>date("Y-m-d H:i:s", STRTOTIME(date('h:i:sa'))));
$this->db->insert('tbl_current_login',$data_login);  
$last_id=$this->db->insert_id();
if (!empty($last_id)) {
    $active_user_session = array('id' => $result->id,'access_role'=>$result->access_role);
    $this->session->set_userdata('active_user_session',$active_user_session);
    return $result;
}

对此

$data_login = array('emp_id' =>$result->id ,'last_activity' =>date("Y-m-d H:i:s", STRTOTIME(date('h:i:sa'))));
$this->db->insert('tbl_current_login',$data_login);  
$last_id=$this->db->insert_id();
if (!empty($last_id)) {
    // $last_id is the id of the tbl_current_login record inserted above
    // pass it to the array below
    $active_user_session = array('id' => $last_id,'access_role'=>$result->access_role);
    $this->session->set_userdata('active_user_session',$active_user_session);
    return $result;
}

然后将您的模型代码更改为此

then change your model code to this

public function update_last_login(){
    $data = array(
                   'last_activity' =>date("Y-m-d H:i:s", STRTOTIME(date('h:i:sa')))
                );

    $this->db->where('login_id', $this->session->userdata['active_user_session']['id']);
    $this->db->update('tbl_current_login', $data); 
}

这篇关于如何使用CodeIgniter在系统中显示实时在线用户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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