Will a Script Continue to Run Even After Closing a Page

Will a script continue to run even after closing a page?

See here:
http://php.net/manual/en/function.ignore-user-abort.php

int ignore_user_abort ([ bool $value ] )

Sets whether a client disconnect should cause a script to be aborted.

When running PHP as a command line script, and the script's tty goes away without the script being terminated then the script will die the next time it tries to write anything, unless value is set to TRUE

There also is a PHP configuration option of the same name:
http://php.net/manual/en/misc.configuration.php

By default, if you do nothing, according to the PHP manual the default is to abort the script.
http://php.net/manual/en/features.connection-handling.php

NECESSARY UPDATE

It seems I (unknowingly) tricked my way to "reputation points", because I did NOT supply the (correct) answer, but here it is now thanks to testing and continued nudging from "mellamokb":

Quote:
"Ok, I took a look at the PHP source code and, if I didn't miss anything, I now have the answer. The "ignore_user_abort" flag is only checked when PHP receive an error trying to output something to the user. So, in my understanding, there is no way to interrupt code which doesn't produce any output."

Okay, I wasn't totally off, but it is important to know that it all depends on whether or not your script produced any output!

If you read THIS, also DO check out the comments below.

Can PHP scripts continue to run even if the user closed the browser?

To answer your question directly, see ignore_user_abort

More broadly, you probably have an architecture problem here.

If many users can initiate this stuff, you'll want the web application to add jobs to some kind of queue, and have a set number of background processes that chew through all the work.

If you close your browser, does PHP code keep running?

PHP is a server-side language. All processing is done on the server, not in the client. Once the request is sent to the server, the client loses control over it.

This means that the server's configuration on timing out will come into effect on such long queries, but closing your browser will have no effect.

If you have used client-side (such as javascript) to submit multiple requests to the server-side language over a 5 minute time period, then yes, closing the browser will in fact interupt that process and prevent it from completing.

A good read about the differences if you wanted to read it:
https://softwareengineering.stackexchange.com/questions/171203/what-are-the-differences-between-server-side-and-client-side-programming

PHP script continues after closing / stopping page

You could use ignore_user_abort but I think it only applies if you are running PHP as a command line script .

When running scripts from browser , in my experience I have realized scripts running even after the browser was closed . I did not go further to check how long they used to run .

The scripts that does huge processing that could run many minutes , I used to have control as below :

  • A flag variable is considered which is stored in a database table .
  • A way is provided to set the flag On or Off ( or 1 or 0 ) .
  • The flag is checked in the script and the script is stopped running if the flag is found to be Off ( or 0 ) .

One way to consider the flag is in a loop as below :

while(true)
{
/* Your code here which probably does lot of processing */

if($flag === false) break; // Or exit if you prefer
}

If you have code that need to run following the loop , you would use break .
If you want to abruptly stop the script you could use exit instead in its place .

force the code to keep running after page closes

If script timeout (max_execution_time setting in php.ini) is no issue for you, you could use ignore_user_abort(true).

But usually the better solution for such tasks is to spawn them in the background. The Gearman extension might be of interest for you, or the different variants of exec.

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

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

PHP script still runs after window is closed?

The fact that your browser is opened or closed makes no difference. Once the PHP process has started, it will keep running until it ends. Browser or no browser.

It's the same as asking "If a tree falls in a forest and no one is around to hear it, does it make a sound?".



Related Topics



Leave a reply



Submit