How can I execute PHP code on page close?

前端 未结 6 1933
心在旅途
心在旅途 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条回答
  •  独厮守ぢ
    2021-01-06 04:04

    If you can use Javascript and JQuery, use 'window.onclose' and then an Ajax call to do whatever you want using PHP:

    Capture event onclose browser

    // Javascript code
    window.onclose = closing;
    
    function closing(){
      $.ajax({
         url: 'yoururl.php',
         data: yourdata,
         success: function(content){
              // empty
         }
      })
    }
    

提交回复
热议问题