Page 1 of 1

Question about ignore_user_abort

Posted: Thu Apr 05, 2007 12:45 pm
by mme000
I think I'm not understanding how "ignore_user_abort()" function works.
Here is an example

Code: Select all

<?php
ignore_user_abort(FALSE);

$myFile = fopen('./myFile.txt', 'w');

for ($i = 0; $i < 15; $i++) {
    sleep(1);
    fwrite($myFile, '*');
}

fclose($myFile);
?>
The problem is that, if I try to browse the file containing this code and I close the browser after a couple of seconds, the execution doesn't stop and, at the end, myFile.txt contains all the 15 '*' characters.
What I'm doing wrong?

Posted: Thu Apr 05, 2007 4:58 pm
by feyd
If you haven't sent anything at the user there's no way PHP can tell if the user disconnected. Even then, it's difficult to really tell.

Posted: Fri Apr 06, 2007 6:57 am
by mme000
Hi feyd,
and thank you for you answer.
You're right, I'm not sending anything to the user, but, closing the browser, I also close the TCP connection to the web server and I thought that it was the web server to forward this information to the PHP engine.
The misunderstanding was also due to what the PHP manual says "ignore_user_abort -- Set whether a client disconnect should abort script execution". It doesn't say anything about user output.
Anyway, does this mean that, if I have a function which doesn't send anything to the user, I can be sure that it will not be interrupted despite of the value of ignore_user_abort?

Posted: Fri Apr 06, 2007 8:06 am
by feyd
If the script doesn't send anything to the user then it shouldn't be accessible (directly) by the user. Set up automation such as cron.

Posted: Fri Apr 06, 2007 5:05 pm
by mme000
You're right again, but I was speaking theoretically.
My question is: if a piece of code doesn't send anything to the user, can be interrupted by a user abort if ignore_user_abort is set to FALSE?
If, for example, I have some code which

1) Send something to the user
2) Executes some code without any output
3) Send something else to the user

is it possible for "part 2" to be interrupted?

Re: Question about ignore_user_abort

Posted: Fri Apr 06, 2007 5:52 pm
by JeFFb68CAM
mme000 wrote:I think I'm not understanding how "ignore_user_abort()" function works.
Here is an example

Code: Select all

<?php
ignore_user_abort(FALSE);

$myFile = fopen('./myFile.txt', 'w');

for ($i = 0; $i < 15; $i++) {
    sleep(1);
    fwrite($myFile, '*');
}

fclose($myFile);
?>
The problem is that, if I try to browse the file containing this code and I close the browser after a couple of seconds, the execution doesn't stop and, at the end, myFile.txt contains all the 15 '*' characters.
What I'm doing wrong?
I don't entirely understand your question - ignore_user_abort is meant to keep the script running even after the user closes his browser session with the page. So, if you close your browser after a "couple of seconds", and it writes all 15 *'s, doesn't that mean the script is working? Or maybe I misread something.

Re: Question about ignore_user_abort

Posted: Fri Apr 06, 2007 6:26 pm
by mme000
JeFFb68CAM wrote:I don't entirely understand your question - ignore_user_abort is meant to keep the script running even after the user closes his browser session with the page. So, if you close your browser after a "couple of seconds", and it writes all 15 *'s, doesn't that mean the script is working? Or maybe I misread something.
Hi, look at the first line of my sample

Code: Select all

ignore_user_abort(FALSE);
Setting ignore_user_abort to FALSE means that you don't want to ignore it.

Re: Question about ignore_user_abort

Posted: Fri Apr 06, 2007 6:54 pm
by JeFFb68CAM
mme000 wrote:
JeFFb68CAM wrote:I don't entirely understand your question - ignore_user_abort is meant to keep the script running even after the user closes his browser session with the page. So, if you close your browser after a "couple of seconds", and it writes all 15 *'s, doesn't that mean the script is working? Or maybe I misread something.
Hi, look at the first line of my sample

Code: Select all

ignore_user_abort(FALSE);
Setting ignore_user_abort to FALSE means that you don't want to ignore it.
Oh, haha, yeah I totally spaced that.

Well as feyd said, it needs to have something sent to the browser in order for it to work properly. You can try adding this inside of your loop.

Code: Select all

echo "\n";
flush();

Posted: Sat Apr 07, 2007 5:02 am
by mme000
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.