Can Closing the Browser Terminate the PHP Script on the Server

Can closing the browser terminate the PHP script on the server?

PHP won't notice that the browser closed the connection unless it tries to output something (e.g. echo). If it fails to output something, the script will be terminated unless ignore_user_abort is On.

So the script will terminate only if the following conditions are met:

  • the script attempts to output something (because the script won't notice that the user aborted the connection until then)
  • AND php's ignore_user_abort setting is off (if the setting is off, php will terminate the script if fails to output something)

You can avoid the script from terminating by enabling ignore_user_abort.

You can use connection_aborted() at anytime to check is the browser aborted the request.

A script can terminate unexpectedly for other reasons (e.g. max execution time, exception, etc); so you should make use of transactions, so that half-finished changes are canceled if the script terminates abnormally.

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

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

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.

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?".

Will Ajax be aborted after the request is done and the browser is closed?

When you close your browser or hit the cancel button, the connection to the webserver is terminated. Whether your server-side php script will continue with execution, depends on the PHP/Apache configuration.

Default configuration is to terminate the execution of the PHP script, as soon as the connection is cancelled. However, you can change this default setting to continue execution!

To do this, please use the ignore_user_abort function:

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

You can read more about the topic here:

http://php.net/manual/en/features.connection-handling.php

There's some more discussion about that here as well:

PHP auto-kill a script if the HTTP request is cancelled/closed

Addition: Just in case, that I am misunderstanding your question: When closing the browser your Javascript code on the client side will of course NOT continue with execution after the PHP script finished.

PHP script with sleep() does not exit on connection close

You can't. Once the HTTP request has been sent off to the server, the PHP script will continue to execute to it's end even if the client has disconnected.

There might be some cases where a script will exit while printing things, but seeing as you're not printing anything it won't exit.

does closing of web browser window cause the termination of php script?

You can use ignore_user_abort to tell PHP to keep running even when the connection is broken.



Related Topics



Leave a reply



Submit