user81993
user81993

Reputation: 6609

Should I always set ignore_user_abort as true?

Until recently I wasn't even aware it was possible for PHP to abort a script due to user disconnect.

Anyways, it could cause some real trouble for my database if the script could just abort midway through. Like if I'm inserting rows into multiple tables that partially depend on each other and only half of it gets done, I'd have to get real defensive with my programming.

Oddly enough, I found that ignore_user_abort defaults to false (at least on my installation), which seems like the sort of thing that could confuse the hell out of developers not aware of this possibility when something goes wrong because of it.

So to make things easier, shouldn't I just always set it to true? Or are there a good reason why it defaults to false?

Upvotes: 1

Views: 945

Answers (3)

WebDevz
WebDevz

Reputation: 29

If your using a relational database, use a transaction to keep data integrity.

Upvotes: -1

Mike Inc.
Mike Inc.

Reputation: 43

Passing true to ignore_user_abort() as its only parameter will instruct PHP that the script is not to be terminated even if your end-user closes their browser, has navigated away to another site, or has clicked Stop. This is useful if you have some important processing to do and you do not want to stop it even if your users click cancel, such as running a payment through on a credit card. You can of course also pass false to ignore_user_abort(), thereby making PHP exit when the user closes the connection.

For handling shutdown tasks, register_shutdown_function() is perfect, as it allows you to register with PHP a function to be run when script execution ends.so it depends on your project

Upvotes: 2

ceejayoz
ceejayoz

Reputation: 180065

Anyways, it could cause some real trouble for my database if the script could just abort midway through. Like if I'm inserting rows into multiple tables that partially depend on each other and only half of it gets done, I'd have to get real defensive with my programming.

This can happen with or without ignore_user_abort, and should be addressed using database transactions.

So to make things easier, shouldn't I just always set it to true? Or are there a good reason why it defaults to false?

Since people are typically writing PHP code for the web, ignoring a user abort means your server would be sitting around doing useless work that's never going to be of value. Enough of them and you might find your server bogged down on abandoned, long-running HTTP requests.

If you've got lots of long-running requests that should ignore a user abort, a queue is a much better approach.

Upvotes: 1

Related Questions