8

How do I write a PHP script that continues running, even after flushing out some text and ending the HTTP request? Is this possible?

1

2 Answers 2

5

To run PHP app forever or until php terminates

ignore_user_abort(true);

set_time_limit(0);
Sign up to request clarification or add additional context in comments.

1 Comment

Seems unsafe if the page is requested frequently, the server could have large number of processes that are doing nothing.
4

You might be interested by the ignore_user_abort configuration directive, and/or the ignore_user_abort function :

Sets whether a client disconnect should cause a script to be aborted.

Using this, you could :

  • generate the part of the page that has to be sent to the browser
  • flush the output, with flush and/or ob_flush
  • call ignore_user_abort (either now or before)
  • do some more work

The user's browser will probably still indicate "waiting" or "loading", but the content of the page will be loaded and displayed -- and even if the user presses "stop", your script should continue its execution.

For more informations, there is an example on the manual's page of that function -- and you can take a look at this article : How to Use ignore_user_abort() to Do Processing Out of Band


Of course, while this can be used for some light process (like "cleaning up" stuff at the end of a page, while displaying it as fast as possible to the user), you'll still be limited by max_execution_time and the like.

So, this is not a solution that should be used for long/heavy calculations.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.