1

While PHP Settings of ignore_user_abort is set to False by default, Assuming i have the following code:

1  1ms   $user->giveMoney(500) 
2  2ms   $user->sendNotification("You got 500")
3  1ms   $user->takeCoins(200)

What if the user aborted the browser after exactly 3ms ? would line 3 be executed?

Ahmed Yusuf
  • 151
  • 3
  • 13

2 Answers2

1

You can control that behaviour with http://php.net/manual/en/misc.configuration.php#ini.ignore-user-abort

At the configuration level (e.g. on a .htaccess file) you can set the flag:

php_flag ignore_user_abort 1

At the script level (valid for that script only) you can call this function:

ignore_user_abort(1)

NOTE: The documentation is a bit misleading. ignore_user_abort works ALSO on the server. See also this page for more references.

Community
  • 1
  • 1
ItalyPaleAle
  • 7,185
  • 6
  • 42
  • 69
1

Yes, it will, seeing as the code you've posted doesn't seem to be producing any output, and you're asking about the behavior in a client-server context. As mentioned in the docs:

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

I've highlighted some key words: this setting deals with PHP when it's being used for command line scripts. You mention a client, closing his/her browser. There's no TTY, so this setting can be false or true, it won't change a thing.

In your case, though, the man states that:

PHP will not detect that the user has aborted the connection until an attempt is made to send information to the client. Simply using an echo statement does not guarantee that information is sent, see flush().

That is to say: the script will keep on running until actual output is really sent to the client. Only then can PHP determine whether or not the connection to the client is still alive. If it isn't then your script will halt.

So unless there is some actual output being sent in that second line of code, and any output buffers are flushed, then yes, the third line will execute. If output is sent, then the script will probably halt.
But if you really want to prevent that third statement from being executed in case of a lost connection, then perhaps call connection_aborted after the second method returns:

$user->sendNotification("You got 500");
if (connection_aborted())
    exit(0);//no error code, just exit
$user->takeCoins(200);
Elias Van Ootegem
  • 74,482
  • 9
  • 111
  • 149
  • ignore_user_abort() can also be used on the server. That line is about "**WHEN** running PHP as a command line script". – ItalyPaleAle Jan 30 '15 at 15:18
  • 1
    @Qualcuno: Was typing up a substantial edit. You're right: it's not CLI specific, so I've edited my response accordingly – Elias Van Ootegem Jan 30 '15 at 15:24
  • What you state in your edit is correct. However, at times it's not easy to know for sure when the server will flush the output to the client. Thus, it can still be a good idea to add `ignore_user_abort(1)` for being certain. – ItalyPaleAle Jan 30 '15 at 15:27
  • @Qualcuno: adding `ignore_user_abort(1)` won't help if you want the script to abort if the user disconnects, though. For that, you'll have to `flush` all buffers, use `connection_status()` and what have you, and _pray_ that it picks up on a lost connection quite quickly (if that's what you really need) – Elias Van Ootegem Jan 30 '15 at 15:29
  • PHP can flush its buffers at times, even without you telling it to. – ItalyPaleAle Jan 30 '15 at 15:32
  • @Qualcuno: Yes, but that wouldn't be a problem. If it flushes the buffers, then it'll pick up on a lost connection. If you `echo`, but the buffers aren't flushed, that's when you won't notice a the disconnect. For that reason, the second quote in my answer ends with _"see `flush()`"_ – Elias Van Ootegem Jan 30 '15 at 15:33