John
John

Reputation: 169

Can we use ignore_user_abort() on any line of code?

Can we use ignore_user_abort on any line of PHP like:

<?php
// Process Codes
if($_GET['nonstop']) {
    ignore_user_abort(1);
    // Background process
}else{
     // Nonbackground process
}
// Other Codes
?>

Or we need to use on only after <?php (first line)?

Thanks.

Upvotes: 1

Views: 374

Answers (2)

Marc B
Marc B

Reputation: 360762

You are able to use it anywhere you want within your code.

Die & Exit

ignore_user_abort() doesn't disable die or exit. It just tells PHP (and the server) to NOT abort the script when the remote users' connection closes.

Worth Noting

"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(). " - PHP.Net

Upvotes: 3

Brad
Brad

Reputation: 163438

It is a PHP function... it needs to be within a <?php ?> block.

If it isn't clear, you can have multiple PHP blocks. You can always stick at the top of your page (and you probably should, if this is the behavior you want!).

Upvotes: 0

Related Questions