I want to display which all user is online (real time; like any chatting module) on my site. I am using the below mention script to do this.
HTML:
<body onbeforeunload="Unload();" onmousedown="checkfunction();">
</body>
Javascript:
var doClose = false;
document.onkeydown = checkKeycode;
function checkKeycode(e){
var keycode;
if (window.event)
keycode = window.event.keyCode;
else if (e)
keycode = e.which;
if(keycode == 17 ){
doClose = true;
}
}
function checkfunction(){
doClose = true;
}
function Unload(){
if(!doClose){
set_user_log_out(<?php ehco $u_id ?>);
}
}
The set_user_log_out
function is to set the user logged out.
In the above mentioned java-script, I wanted to consider the below mentioned scenario.
- The user may open more than one tab in the browser. Or, he may open more than one browser (same browser only).
- The user may close one tab and visits the other tab.
- The user may clicks on one tab and simply closes the other tab.
- The user may close the browser without clicking on log-out option.
- The user may close the browser using keyboard short-cut (for eg. ctrl + F4).
So, I was checking the keycode (which key has been pressed using keyboard) as well. But, there is some issue with this code. Mentioned them below.
- I have not found any solution to get the keycode of mulitple keys (eg. ctrl + F4)
- Considering two tabs, if I type other url ( of the same site), the page keeps loading in firefox.
- ctrl + r does not work fully.
- Sometimes while trying to visit other page of the site, it simply executes the function
set_user_log_out()
(happens in firefox only)
Please suggest what I need for this. I have also gone through the below mentioned url.
How to Track the Online Status of Users of my WebSite?
But the function described on the above url does not consider my scenarios.