Why in Chrome, when I hit "STOP" button when PHP script is executing, it does not stop execution? Even closing TAB doesn't stop it. ignore_user_abort()
is false
. Any ideas on how to force PHP to stop? Got large script, that makes some big files and executes for like 10 minutes...

- 11,672
- 8
- 47
- 91
-
2hitting esc in a browser only shuts down the tcp connection to the server. PHP cannot detect that shutdown until it tries to dump output to the now-closed connection and gets told it's closed. – Marc B Apr 24 '14 at 18:06
-
[This answer](http://stackoverflow.com/questions/15991113/php-doesnt-detect-connection-abort-at-all/15992649#15992649) to a previous question has some suggestions of things to try/look at in this situation. – cincodenada Apr 24 '14 at 18:09
-
if you are processing large files then logically it should take long why would you want to abort your process? – user2009750 Apr 24 '14 at 19:31
-
because those files are zip archives that are being created from large database, and some may simply want to abort request, as it uses memory, space and cpu time of server... – Flash Thunder Apr 24 '14 at 20:33
2 Answers
There is no way for a user to stop a PHP script. Since PHP is run on the server when a page is called, you cannot stop it unless you have a condition in your script that will quit to your liking.

- 793
- 9
- 18
-
1See the PHP docs for [ignore_user_abort()](http://us3.php.net/manual/en/function.ignore-user-abort.php) and [Connection Handling](http://www.php.net/manual/en/features.connection-handling.php), particularly this bit: "If the remote client disconnects, the ABORTED state flag is turned on. A remote client disconnect is usually caused by the user hitting his STOP button...you can decide whether or not you want a client disconnect to cause your script to be aborted." PHP clearly is intended to provide this behavior. – cincodenada Apr 24 '14 at 18:11
-
OP as stated that he tried to use `ignore_user_abort()` and that it wasnt working. Also, The script will die out only if the script tries to output something to the client. Meaning that if the script use `ob_start()` or doesn't echo anything , the script will continue to run until it echo something to the user. Most modern framework usually cache the output and display result at the end which would render the command obsolete. – NMC Apr 24 '14 at 18:18
-
Everything in your comment is true, but that hardly means "there is no way for a user to stop a PHP script". That is plainly false: the mechanism is there, but OP has to make sure the right conditions are there to trigger it. – cincodenada Apr 24 '14 at 18:19
-
Which are? . . . your answer seems to have no sense... sorry. Of course that PHP should stop script when disconnected. It should be default behaviour, but somehow it's not... – Flash Thunder Apr 24 '14 at 20:30
-
You have to output some non-buffered text to the client. When you do that, PHP checks if the client is still connected and will die if the `ignore_user_abort()` is `false`, and continue the script if it's `true`. EX: just output a aspcae as a string `echo ' ';` – NMC Apr 25 '14 at 13:32
-
As I mentioned in a comment, this answer to a previous question has a lot of info on this.
But one takeaway that may be your issue: PHP may not know it's disconnected until it tries to send data and is refused, see this bit in the docs:
the next time your script tries to output something PHP will detect that the connection has been aborted
So depending on your required output, you may be able to send some sort of "heartbeat" data to the browser that will trigger the abort if the user disconnects. If it doesn't seem to be sending, try doing some explicit buffer-clearing with flush()
. If you (or your framework) are using output buffering, you may have to work around it.
If you can't do that (if you're generating an output file or some such), you'll likely have to rearrange things, since there's no way PHP can know the connection is closed. My suggestion is to use a queueing system to offload the generation of things to a separate script that you can then cancel/kill manually - here's a good overview of queueing systems, I personally use beanstalkd with PHP - it's simple, easy, works splendidly, and has some good PHP libraries (I've used pheanstalk and davidpersson's beanstalk). Any time you're generating large files like that, you should probably be using a queueing system anyway.

- 1
- 1

- 2,877
- 25
- 35
-
Ok, but mostly long time scripts don't output anything... mine is for example generating zip file, and it can't output anythng before finish, or downloaded zip will be corrupt. – Flash Thunder Apr 24 '14 at 20:29
-
Which is why I said "you *may* be able to". If you're generating a zip file, then it's a little trickier. Read up on the post I linked, but you'll likely have to rearrange things in this case, because there's no way for PHP to know the client has disconnected. Use a queueing system or some such, which is best practice for stuff like this anyway. – cincodenada Apr 24 '14 at 21:14