My issue is when the user closes the tab then I have to call the ajax to update the logout time in the database. So I tried below code after some research but still, it's not working.
I notice that. It's automatic log out after 20 sec after adding the below script in the JS.
I used below code but this is not a correct code as per suggested by SO users team.
window.onbeforeunload = function(){
// var msg="Are you sure want to close this tab";
// return msg;
$.ajax({
method:"POST",
async: false,
url:baseUrl+"/Employee_control/logoutTimeUpdate"
});
so I change it to this but still not working.
var _wasPageCleanedUp = false;
function logoutTimeUpdate()
{
if (!_wasPageCleanedUp)
{
$.ajax({
type: 'GET',
async: false,
url:baseUrl+"/Employee_control/logoutTimeUpdate",
success: function ()
{
_wasPageCleanedUp = true;
alert("hello");
}
});
}
}
$(window).on("unload", function ()
{
logoutTimeUpdate();
});
Controller
public function logoutTimeUpdate(){
$updatedLogoutTime=$this->Employee_model->logoutTimeUpdate();
if ($updatedLogoutTime == 1) {
$this->session->unset_userdata('login_session');
$this->session->sess_destroy();
}
}
}
Model
public function logoutTimeUpdate(){
$data = array('logout_time' =>$this->current_date);
$where = array('login_id'=>$this->session->userdata['login_session']['id']);
$this->db->where($where);
$this->db->update('tbl_current_login', $data);
return 1;
}