0

Asked many times, the usual response is to use buffering and HTTP "Connection: close" with "Content-lenght" headers:

How do I close a connection early

The problem is that this has several downsides:

  • It fails for larger output (larger than output buffer)
  • It depends on the browser to actually close the connection (which may also create a security risk because PHP will send any data anyway even if the "Connection: close" header was sent)
  • It makes the website slower (client can start rendering even when output is not yet complete, with output buffering you have to generate the whole output and can only then start to send it to the client - so instead of generating the output and sending it in parallel, you can only start sending it after generation)
  • It forces separate connections for subsequent requests
  • It messes up compression

Forking a background process by invoking php via system() is inefficient because everything (database connection, loading of data, etc.) has to be repeated.

Is there a way to just close the connection and keep running without buffering?

In the docs it reads:

Closing the users browser connection whilst keeping your php script running has been an issue since [PHP] 4.1, when the behaviour of register_shutdown_function() was modified so that it would not automatically close the users connection.

I also use register_shutdown_function() - is it maybe possible to close the connection in the shutdown function?

Roland Seuhs
  • 1,708
  • 4
  • 23
  • 47

0 Answers0