title: How to get the user online status (real time) in my website
body: <p>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.</p>
  
  <p><strong>HTML:</strong></p>
  
  <pre><code>&lt;body onbeforeunload="Unload();" onmousedown="checkfunction();"&gt;
  
  &lt;/body&gt;
  </code></pre>
  
  <p><strong>Javascript:</strong></p>
  
  <pre><code>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(&lt;?php ehco $u_id ?&gt;);
   }
  }
  </code></pre>
  
  <p>The <code>set_user_log_out</code> function is to set the user logged out.</p>
  
  <p>In the above mentioned java-script, I wanted to consider the below mentioned scenario.</p>
  
  <ol>
  <li>The user may open more than one tab in the browser. Or, he may open more than one browser (same browser only).</li>
  <li>The user may close one tab and visits the other tab.</li>
  <li>The user may clicks on one tab and simply closes the other tab.</li>
  <li>The user may close the browser without clicking on log-out option.</li>
  <li>The user may close the browser using keyboard short-cut (for eg. <kbd>ctrl</kbd> + <kbd>F4</kbd>).</li>
  </ol>
  
  <p>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.</p>
  
  <ul>
  <li>I have not found any solution to get the keycode of mulitple keys (eg. <kbd>ctrl</kbd> + <kbd>F4</kbd>)</li>
  <li>Considering two tabs, if I type other url ( of the same site), the page keeps loading in firefox.</li>
  <li><kbd>ctrl</kbd> + <kbd>r</kbd> does not work fully.</li>
  <li>Sometimes while trying to visit other page of the site, it simply executes the function <code>set_user_log_out()</code> (happens in firefox only)</li>
  </ul>
  
  <p>Please suggest what I need for this. I have also gone through the below mentioned url.</p>
  
  <p><a href="https://stackoverflow.com/questions/1084099/how-to-track-the-online-status-of-users-of-my-website">How to Track the Online Status of Users of my WebSite?</a></p>
  
  <p>But the function described on the above url does not consider my scenarios.</p>