Reputation: 2388
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?
Upvotes: 1
Views: 3261
Reputation: 14614
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);
}
Upvotes: 1