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?
I just want to display the live online user in the system. Would you help me out with this issue?