I am trying to find a method to execute some PHP code once a user closes the page. In my application, once the user closes or navigates away from the page the server will state the user as ‘Offline’ in the database. This requires the code to know when the user has navigated away from the page. My application also has an endless load (i.e. it will sleep until the user closes the page).
Share
Reliably sending an event when the user closes the page is close to impossible. There is the
onbeforeunloadevent that could do an Ajax call, but that could fail, not be executed for security reasons, or not be executed at all because the user has JavaScript turned off.This is usually done through session timeouts: It is checked when a request for a certain session was last made. If it was longer than x minutes ago, the session is treated as expired. Is that not an option?