[ACCEPTED]-PHP: ignore_user_abort(true) in all scripts-abort

Accepted answer
Score: 14

For your specific example, using database 27 transactions (as mentioned by Ignacio) would 26 be the more suitable approach.

There are 25 other cases where you might want to make 24 sure the user can't abort early, though, not 23 relating to databases. For example, if you 22 update the database and then send out a 21 mail, you don't want a user to be able to 20 stop the process before the mail goes out. In 19 this sort of case, ignore_user_abort would be appropriate.

However, note 18 that a broken pipe due to client aborting 17 the connection doesn't stop execution right away, only 16 at the point you next try to write to the 15 script output. This can be through calling 14 echo or print, or even just by closing the PHP tag 13 and inserting some whitespace before opening 12 a new one (... ?> <?php ...). So if you have all the ‘action’ part 11 of your script at the top of the page, before 10 you try to write any page content, you don't 9 have to worry about interruptions from broken 8 pipes affecting your app logic.

And of course 7 you should be separating action logic from 6 page content in that way anyway.

From http://php.net/manual/en/function.ignore-user-abort.php#refsect1-function.ignore-user-abort-notes

PHP 5 will not detect that the user has aborted 4 the connection until an attempt is made 3 to send information to the client. Simply 2 using an echo statement does not guarantee 1 that information is sent

Score: 1

If you need multiple queries to either happen 7 completely or not at all, then you should 6 be doing it properly using transactions 5 instead of putting a bandage on the problem.

It 4 can be handy for the remote side to abort 3 a request if e.g. the request involves lengthy 2 calculations and the user decides that they 1 don't need the results after all.

Score: 1

You can set ignore_user_abort in php.ini: PHP.ini configuration

I wouldn't do this, though. I 8 would much rather have the PHP page start 7 another PHP instance on the command line 6 that does the querying in the background, e.g. using 5 exec.

Also, it could be that your basic flow 4 concept is skewed. If the user aborts the 3 execution of a calculation, you should probably 2 roll it back properly anyway - but that 1 depends on the nature of what is being done.

More Related questions