Search code examples
phpcodeignitersessiontimeoutonbeforeunload

Perform some event when php session timeout


I tried it with jquery 'onbeforeunload',but failed as jquery doing that action even on refreshing page. All i want to do is ,perform a db function just before any user close his browser or computer.So that i can know he has logged out.I want to add facebook like functionality "Logged in from other devices" ,with a forcefull logout from other devices.


Solution

  • There's no way to catch the on browser close event in JavaScript. You can try web socket and CRON job to achieve your wants.

    For web socket, you can get the total connections of specific user, and if the connection becomes 0, it means that the user closed all the pages(window or tab) of your web app. Also, you need to keep in your mind the internet disconnection, by this, you need to use CRON job, this CRON job must run every specific minutes (ex: 15 mins), so that if the user lost a connection for 15 minutes, you must automatically log out that user.

    If you don't have rules like user must be disconnected after several minutes of idling, then you can just simply use web socket.