How To Display The Live Online User In The System Using 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.in
Solution 1:
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
publicfunctionupdate_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 =8WHERE 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
publicfunctionupdate_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);
}
Post a Comment for "How To Display The Live Online User In The System Using Codeigniter?"