You've probably heard of the setting in php.ini ignore_user_abort, and how it allows you to ignore a user aborting page load and thus terminating your script mid way through. Today I found out that didn't quite mean what I thought that meant.

I was under the impression that when the user hit stop, the script stopped. Which is: bad, and wrong. PHP doesn't detect that the user has terminated the connection, it has no clue, it obligingly continues processing along, until it attempts to send information to the user. Now, just because you have an echo statement, doesn't mean that information is necessarily being sent to the user, buffering (not necessarily output buffering, PHP and your web server also implement some buffering) will probably catch it. It's not until that buffer is filled, or you call something like flush() that information is actually sent, and thus PHP would know the user has aborted.

When might you want to ignore a user abort? Well, simply put, any situation where you're executing a series of sequential actions and stopping in the middle would leave your application in a bad state (transactions are the database solution for this, they're simply rolled back if you disconnect without committing (unless you're using a persistent connection)).

In short, the user aborts when they close their browser, hit stop, etc. PHP however doesn't know the user has aborted until it actually attempts to send them data.

Comments »

Weblog: PHPDeveloper.org
Tracked: Oct 03, 12:26
PHP >> ignore_user_abort
If you’re looking for a way to keep a PHP script running on the server even after the user navigates from the page or hits the stop button, you can use the “ignore_user_abort” flag in the php.ini file or set it using ini_set function...
Weblog: VT's Tech Blog
Tracked: Oct 03, 14:26
Thank you for clarifying it. Hopefully, in almost all projects I prefer to build the whole HTML and then send it to the user in one echo statement.
#1 Alex@Net (Homepage) on 2007-10-03 00:05 (Reply)

Also it's worth mentioning that Paul updated the PHP Manual with this information... :-)
#2 philip olson (Homepage) on 2007-10-04 01:08 (Reply)

Should I use this around a file open and file close? What if the user aborts after opening a file, but before closing? Could that cause a memory leak?
#3 Adam (Homepage) on 2008-07-07 13:33 (Reply)

Adam, That's not really a concern.

PHP automatically closes any open files when it shuts down (also releasing any locks you might have placed) so you don't have to worry about things left hanging.

That being said, there is one possible concern, if you're writing data to a file, and echoing out data in the middle, you could end up with a file that only has a subset of the amount of data you want it to contain.
#4 Paul Reinheimer (Homepage) on 2008-07-07 13:59 (Reply)


Enclosing asterisks marks text as bold (*word*), underscore are made via _word_.
Standard emoticons like :-) and ;-) are converted to images.
 

Hi, I’m Paul Reinheimer, a developer working on the web.

I wrote a book titled Professional Web APIs with PHP back in 2006, and am currently working in Biomedical Informatics for a major public health company.

I’m working on a project to help developers called WonderProxy which has proxies all over the world. Working on GeoIP development? Now you can finally test properly! We've also released Global Ping Statistics for expected ping times between cities, as well as a Load Testing Tool to measure your site's ability to handle load. Our most recent site checking tool is Where's it Up? which checks your sites availability globally, returning HTTP, DNS, and Traceroute details

My hobbies are cycling, photography, travel, and engaging Allison Moore in intelligent discourse. I frequently write about PHP and other related technologies.

I co-founded:

WonderNetwork Logo

Search