dongsheng4679 2018-09-16 04:03
浏览 81
已采纳

如何使用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

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

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

Here is my code.

Login Password verify

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
               }?>
             });

Employee controller

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

Employee model

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); 
    }

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?

enter image description here

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

  • 写回答

1条回答 默认 最新

  • dongshi6969 2018-09-16 05:04
    关注

    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);
    

    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); 
    }
    

    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
    

    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;
    }
    

    to this

    $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); 
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 我的代码无法在vc++中运行呀,错误很多
  • ¥50 求一个win系统下运行的可自动抓取arm64架构deb安装包和其依赖包的软件。
  • ¥60 fail to initialize keyboard hotkeys through kernel.0000000000
  • ¥30 ppOCRLabel导出识别结果失败
  • ¥15 Centos7 / PETGEM
  • ¥15 csmar数据进行spss描述性统计分析
  • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗