How can I execute PHP code on page close?

前端 未结 6 1944
心在旅途
心在旅途 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
    2021-01-06 04:12

    Simply set ignore_user_abort(true) then enter an infinite while loop of

        while (!connection_aborted()){
          // do nothing ...
        }
    
    //after the while loop :
    some_function_to_say_that_the_user_is_offline();
    

    but be carefull , whenever the user navigates out of the page , the function WILL be executed .

    EDIT : and the progress bar of the browser will be like "downloading ..." forever ...

    Edit 2 : check also http://www.php.net/manual/en/function.register-shutdown-function.php

提交回复
热议问题