Suppose I make an AJAX HTTP Request from jQuery to a backend PHP script. The request is made, the PHP script starts running and doing its magic. Suppose I then change to another website, away from the site where the original AJAX Request was made. As well, I do this before the PHP script finishes and has time to do a HTTP Response back. Does the PHP script finish running and doing its thing even though I’ve switched to another website before I got the HTTP Response?
So the order is this.
- I’m on website http://www.xyz.com
- I have a jQuery handler that kicks off an AJAX request to blah.php
- blah.php starts running
- I go to website http://www.abc.com soon after without waiting for a response from blah.php
What’s going on with blah.php? Is execution still going on? Did it stop? I mean it didn’t get a chance to respond so…
This may depend on your server configuration, but in general the script will continue to execute despite a closed HTTP connection.
I have tested this with Apache 2 + PHP 5 as mod_php. I would expect similar behaviour with PHP as CGI and with other webservers but do not know for certain.
The best way to determine for certain on your configuration is, as @tdammers suggests: set up a test script something like the following and monitor the log.
Access this script (at
/test.phpor whatever) then before you get any results, hit stop on your browser. This is equivalent to navigating away before your XHR returns. You could even have it as the target of an XHR and navigate away.Then check your error log: you should have a start and then messages every 10 seconds for two minutes and an end. You can modify how high
$igets to ensure your script will reach its anticipated maximum execution time if you’d like to test that too.You don’t have to use
error_log()– you could write to a file, or make some other persistent change on the server that can be checked without needing to keep the client connection open.The script execution time may stop before then because of the
max_execution_timephp.ini directive – but in any case this should be distinct from when the webserver times out.