I have 2 pages. One is form.php and ajaxprocessing.php
When user clicks on submit button on form.php, it will call the ajaxprocessing.php using ajax. This ajaxprocessing.php has a loop of say 50,000 times to do database insert query or some function.
My question is, even when, if the user clicks the submit button in form.php and closes the browser or shut down the PC, will the ajaxprocessing.php will still continue to execute in server?
How does it works behind the scene?
By default, PHP execution will terminate if the client disconnects. So if your ajax call times out or disconnects, then the PHP will stop running. This behavior can be changed by turning on the configuration setting ignore_user_abort
.
From the comments in php.ini:
; If enabled, the request will be allowed to complete even if the user aborts
; the request. Consider enabling it if executing long requests, which may end up
; being interrupted by the user or a browser timing out. PHP's default behavior
; is to disable this feature.
; http://php.net/ignore-user-abort
A way to test if the script keeps running would be to insert something like this in the bottom of your PHP script.
mail('your@email.com', 'Script done', 'The script is now done');
This will send you an e-mail when the script is done - if sendmail is installed on the server of cause. This way you will know if the script keeps running after you close the browser.