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

I am calling a php script over ajax to do some database maintenance. If the user closes the page, hits back, or clicks a link, will the php script be fully executed? Is there a way to do it?

Maybe if the php script called the exec() method or something similar, which would in turn call a script via the console as such:

$ php /var/www/httpdocs/maintenance.php

?

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

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.

Solution 2

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.

Solution 3

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.

Solution 4

Varies on the settings, web server, operating system and so on.

Usually the request will be processed as usual, and the response will just never be read. Occasionally, a write might fail earlier, and the request fails while processing.

Solution 5

Once the ajax call is kicked off, the user is free to do whatever they want. If they close the page they simply won’t get the feedback (if any ) from the ajax call that was made.

Solution 6

If the php starts executing then it will continue to execute regardless if the user closes the window or navigates away from the page.

Solution 7

The php script will complete, regardless of browser state. The php is parsed on the server, and that doesn’t care about whether the client is still open or not.

Solution 8

If the HTTP request was completed, then yes, the PHP script will be executed fully even if the client’s computer is closed.

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply