Question about ignore_user_abort

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mme000
Forum Newbie
Posts: 7
Joined: Wed Apr 04, 2007 11:14 am

Question about ignore_user_abort

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
mme000
Forum Newbie
Posts: 7
Joined: Wed Apr 04, 2007 11:14 am

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
mme000
Forum Newbie
Posts: 7
Joined: Wed Apr 04, 2007 11:14 am

Post 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?
JeFFb68CAM
Forum Newbie
Posts: 15
Joined: Tue Apr 03, 2007 11:17 pm

Re: Question about ignore_user_abort

Post 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.
mme000
Forum Newbie
Posts: 7
Joined: Wed Apr 04, 2007 11:14 am

Re: Question about ignore_user_abort

Post 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.
JeFFb68CAM
Forum Newbie
Posts: 15
Joined: Tue Apr 03, 2007 11:17 pm

Re: Question about ignore_user_abort

Post 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();
mme000
Forum Newbie
Posts: 7
Joined: Wed Apr 04, 2007 11:14 am

Post 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.
Post Reply