Reputation: 233
If an ajax request is made to server to run a script in backend and user closed the browser. Will that script complete execution on backend if it started and was in midprocess?
Upvotes: 7
Views: 3245
Reputation: 4682
NO.., PHP script will not terminate its execution.
YES.. it'll complete its execution.
Once php script is initiated, it'll complete its execution and then will stop.
because, php runs at server side, it can't be interrupted by client side simple event like browser window close
.
But however client will not be able to see the output.
for ex:
Try This Code:
//File Name: xyz.php
<?php
$fp=fopen("output.txt","w");
$count=0;
for($count=0;1;$count++){
//Infinite loop
fwrite($fp,"".PHP_EOL."".$count."");
sleep(1);
if ($count>=60) break;
}
fclose($fp);
?>
and now test this file, Even after you close the browser window, it'll continue to print inside that output file (output.txt
).
Upvotes: 9
Reputation: 162
Well, PHP is server side, it computes all the code and then sends the results to the client. So I guess it finishes the script execution even if you close the browser.
Upvotes: 2