Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

javascript

php

if i call a php file via jquery ajax, that contains a script to do some stuff that takes a while — for instance uploading a big video — and then I close the page: does the php script keep loading the video or not?

like image 793
Francesco Avatar asked May 13 '11 19:05

Francesco


People also ask

What happens when browser is closed?

Browsers deletes the session cookies when the browser is closed, if you close it normally and not only kills the process, so the session is permanently lost on the client side when the browser is closed.

Can a PHP script run forever?

You can make it run forever by either setting the value or call set_time_limit in your script (http://php.net/manual/en/function.set-time-limit.php).

Does PHP run in browser or server?

Chapter 1. PHP: What, Why, and Where? PHP is ultimately just text that is taken by your web server and turned into a set of commands and information for your web browser. And because you're just working in text, there's not a lot you have to do to get going as a PHP programmer.


2 Answers

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.

like image 163
Mörre Avatar answered Sep 21 '22 16:09

Mörre


A PHP Script running through a web server will not stop until:

  • someone kill the server
  • the server kill the php scrip

When the user abort the script, PHP will continue until it try to send something back to the browser.

For example still script will continue fore ever even if the user abort:

while(true){     echo 'go'.PHP_EOL; } 

It will go on forever because the "echo", will write into the buffer, and the buffer will not be sent to the browser until the script finish, which will never happen.

The following script will stop as soon as the user abort:

while(true){     echo 'go'.PHP_EOL;     flush();     ob_flush(); } 

This script will stop, because flush() and ob_flush() will force PHP to send its buffer to the browser, which will stop the PHP script if the user has aborted. The function ignore-user-abort() will force PHP to ignore the abort in this case.

Moreover if you are using PHP session, they are another tricky situation. For example, if you are doing AJAX, and you actually send two AJAX request to a PHP script and that PHP script has need of session with session_start(). The first AJAX query will work normally, however the second one will have to wait until the first call is finish, because the first script has a locked on the session. The first script could eventually prematurely release the session with session_write_close();

like image 23
zzarbi Avatar answered Sep 23 '22 16:09

zzarbi