What happens if a user exits the browser or changes page before an AJAX request is over What happens if a user exits the browser or changes page before an AJAX request is over php php

What happens if a user exits the browser or changes page before an AJAX request is over


It's a race condition. PHP will detect at some point (usually upon attempting to do output) that Apache is yelling in its face that the remote user has closed the connection. Whether everything you wanted to do is done at that point depends on how your code's structured.

If you want to ensure that all operations are complete before the script shuts itself down, use ignore_user_abort(TRUE), which keeps PHP running after the connection is severed. It's still subject to the user max_execution_time limits and whatnot, but it will not shut down because you disconnected.


As long as the user agent (browser, etc.) has fully sent the request, the server has all it needs and will complete the request and try to send back a response.

In fact, this sort of "pinging" behavior is often used for "heartbeat"-like processes that keep a service warm or perform periodic maintenance.


Once the web request makes it to your server, it really doesn't matter if the user closes their browser or navigates away. Your server will still respond, but no one will be listening for the response.