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 st
Page requests are stateless, meaning you'll never have a 100% working detection method to determine if someone has left the page. You can try to catch the page unloading, but this isn't 100% accurate. You could also try as @Nayena mentioned with AJAX, but this is less than ideal.
Your best bet is to do like most common sites do, use "last activity within N minutes" as an indicator and not try to catch when they navigate away or close the page.