How can I execute PHP code on page close?

前端 未结 6 1932
心在旅途
心在旅途 2021-01-06 04:01

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

6条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-06 04:26

    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.

提交回复
热议问题