0

This happens currently:

  1. Somebody requests https://www.example.com/
  2. My PHP script does it things, including many SQL queries, adding entries to an array for each.
  3. I run my "flush database log" function, which loops through that array and actually inserts the SQL queries into my database query log.
  4. The script finishes, thus sends all the HTML output to the client.

To prevent the client having to wait for longer than necessary, I would like this to happen instead:

  1. Somebody requests https://www.example.com/
  2. My PHP script does it things, including many SQL queries, adding entries to an array for each.
  3. The script sends the output to the client.
  4. I run my "flush database log" function, which loops through that array and actually inserts the SQL queries into my database query log.
  5. The script finishes, with the user not having to wait for the "flush database log" function, which is executed after the page has loaded from the user's perspective.

I know about the various flush, ob_flush, ob_flush_clean and 800 different functions like that in PHP. I also know that the user comments and general online advice is full of warnings about how this either no longer works, or has never worked in the first place, apparently depending on the webserver/OS of the server.

Could I please, once and for all, get a 2020 answer to whether or not this works? And, if it does work, which function exactly am I supposed to use to "just send the webpage already; I'm done as far as the user is concerned"?

  • Yeah, this used to be possible. I don't remember the details... it was a decade ago, but I think I did a simple fork. Usually, it'd just queue up a job though for some background daemon to handle, that way I could asynchronously track progress. – Brad Oct 24 '20 at 01:08
  • The flush functions are what you're looking for. You can output everything to the user and the script continues to run in the background. It's not perfect though, you may want to look at something other than PHP. – Jay Blanchard Oct 24 '20 at 01:17
  • @JayBlanchard Please specify what about it is "not perfect", which is what my question is about. And what do you mean by "looking at" something "other than PHP"? – user14430182 Oct 24 '20 at 01:48

0 Answers0