简体   繁体   中英

Using Jquery unload function to delete user from online users list

I want to know is there a way correct way to delete user from online users list with $(window).unload jQuery function? or keep using PHP to check it? i think this is not a correct way because user can edit it, but i need more reasons. Code example:

<script src="jquery.min.js"></script>
 <script>
 $(window).unload(function() {

   $.ajax({ 
   type: "GET", 
   url: 'http://localhost/delete_session.php', 
   data: "id=" + "session id";
   });
});
</script>

Thanks!

Doesn't work well with multiple tabs : if your the user opens your app in two different tabs, closing any of the two tabs will delete the session, and the other tab will become "unusable".

You should not solely rely on the client's behaviour to manage your sessions : the client's browser may exit incorrectly (program bug, battery out, kill -9 , etc...) and not execute the unload callback on exit.

A correct way to do online list is probably using heartbeat requests.

Your javascript sends small requests to server indicating they are "alive".

If all pages are closed by user, the heartbeat stops and you somehow remove it from online user list on the server.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM