0

I have a webform that sends data to PHP script.

PHP script may take a while to process the data. What I want to do is to send raw data to database, then redirect the visitor to "thank you" page and then continue processing the data in background. Important thing is that the script must continue working even if the visitor closes "thank you" page.

Can you advise which solution should I look into?

P.S. I use nginx + php-fpm if that matters.

UPDATE. I've found info about using ignore_user_abort(true). Could this be the way to go?

8
  • AJAX might be a solution for you. Commented Oct 21, 2014 at 12:22
  • stackoverflow.com/questions/45953/… Commented Oct 21, 2014 at 12:23
  • Define "a while". Seconds? Minutes? Hours? Long enough that the user might close the browser and leave before it's done? Commented Oct 21, 2014 at 12:24
  • 2
    @JayBlanchard ajax would cause the request still to be handled synchronously at the server side, making the ajax request time out. Doing a background process or a cron job would be more feasible Commented Oct 21, 2014 at 12:24
  • How to Run PHP Code in the background might help. Commented Oct 21, 2014 at 12:26

3 Answers 3

3

What I want to do is to send raw data to database, then redirect the visitor to "thank you" page and then continue processing the data in background.

That basically describes how I'd do it right there, actually.

Consider two separate applications. One is the web application, which saves the user input to the database and then continues to interact with the user. The other is a scheduled console application (a standalone script invoked by cron most likely) which looks for data in the database to be processed and processes it.

The user uploads the data, receives a "thank you" message, and his/her interaction is complete. The next time the scheduled task runs (every couple minutes, maybe?) it sees the pending data in the database, flags it as being processed (so if another instance of the script runs it doesn't also try to process the same data), processes it, flags it as being done (so it doesn't pick it up again next time), and completes.

You can notify the user of the completed process a couple of different ways. The back-end script can send the user an email (active notification), or perhaps the web application can examine the table for the flagged completed records the next time the user visits the page (passive notification).

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the suggestion! I will think about this approach with cron.
One more thanks because this approach solves another problem - if the external API used in processing is unavailable for some reason, the cron job will keep trying to send the info every time it runs. Great solution!
@Ubertone: Depending on how the logic is set up, there's a lot you can do fairly easily by separating the concerns like this. As you say, an automatic re-try is a significant benefit. You can also mark specific records as "failed" and continue with the remaining records, so a user's data doesn't entirely fail processing just because of one bad record. And so on.
0

Something like this should work :

pclose(popen('php script.php &', 'r'));

https://www.php.net/manual/fr/function.popen.php

You can also use more options or others functions to get more control over the execution :

https://www.php.net/manual/fr/function.proc-open.php

But use this carefully and be sure you need this way to resolve your problem.

Ajax would be nice.

Comments

-1

You need to do the thing asynchronously. Use AJAX to achieve this

6 Comments

Ajax would still have the request be handled synchonously thus making the ajax request wait for the result.
This doesn't really explain much and kind of treats "AJAX" as sort of a magic wand. Probably better as a comment than an answer.
@DoXicK: You are correct technically. It actually depends what further process will be involved in this scenario. Don't understand why this answer is downvoted.
@vsingh: It was probably downvoted because it doesn't really answer the question. "Use AJAX" isn't an answer, and doesn't do much to address the question being asked.
i guess it got downvoted since it doesn't give an actual answer, like david already said. It's like saying: 'how do i get from A to B?' -> "walk". While by definition not a wrong answer, it's not usefull either :-)
|

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.