How to make sure the user cannot interrupt executi

2019-08-28 21:43发布

How can I make sure that some code, that is executed in a php shutdown function, that was registered via "register_shutdown_function" is also executed, if the user leaves the page or closes the tab or the whole browser of that page, before the shutdown code is executed, but after the page is displayed?

I just want to make sure the user cannot interrupt the shutdown code, once the page is displayed.

I refer to this:

http://www.php.net/manual/function.register-shutdown-function.php#92959

And do you think the code of Filip Dalge in this comment on php.net is the correct way?

Is it necessary for all php versions (only interested in >= 5.0) ? How (if so) does this behavior change in the different versions?

标签: php callback
1条回答
狗以群分
2楼-- · 2019-08-28 21:52

The shutdown function will always be executed (unless there is a fatal PHP error), so you don't need any extra code. The code in that comment is to force the web server to flush all buffers and send the generated data to the browser before executing the code. I wouldn't use it if you plan to use register_shutdown_function() elsewhere (or if any library you need makes use of it), since it changes the state of the application - another shutdown handler might expect an open connection to the browser, for example.

查看更多
登录 后发表回答