Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Will closing the browser prevent a PHP script from finishing execution?

Tags:

html

php

apache

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?

like image 728
Uday Hiwarale Avatar asked Sep 05 '14 09:09

Uday Hiwarale


2 Answers

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).

like image 61
Vedant Terkar Avatar answered Sep 19 '22 12:09

Vedant Terkar


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.

like image 33
Andrea Avatar answered Sep 19 '22 12:09

Andrea