0

I would like to find a way to have my user not having to wait for the output of a php script and being redirected to a page while the script is running on the server.

Basically the user submits a form which takes quite long to process and I would like to redirect the user to a page notifying him that the form is being processed and that its output will be later available (I thought about opening a tab when the output is ready).

Basically I would like something like this, which I tried without success, the

if ($form_valid) {
   process_form(); // this would need not to be running on the current page so that the user don't have to wait for it to be ready (timeout problems)
   header('Location: http://example.com/form_submitted_output_coming_soon.html');
}

I hope that it is not too vague.

Thank you in advance for any help / advice on how I could do that.

hakre
  • 193,403
  • 52
  • 435
  • 836
Iam Zesh
  • 1,797
  • 2
  • 20
  • 42
  • Do you want the user to be able to perform other tasks in the website while you are processing his request or do you want to simply give him some sort of indication (eg: loader) that his request is being processed? – Ifthikhan Oct 15 '12 at 08:03
  • Humm, at best it would be good if he could do something else – Iam Zesh Oct 15 '12 at 08:15

3 Answers3

1

The best approach for solving this kind of problem is to use AJAX to make the request to the server in the background and then update the user once it has finished processing.

Mitch Satchwell
  • 4,770
  • 2
  • 24
  • 31
1

You may submit the form with an asynchronous request (ajax) and handle the page forward also with javascript. This way your form is handled asynchronously - you may even wait for the response to tell the user once you have an answer. This asynchronous request will not block the UI.

Just for completeness if you really really want to use php only for this: Run PHP Task Asynchronously

Community
  • 1
  • 1
eX0du5
  • 896
  • 7
  • 16
1

It really depends on the time the script takes to execute if it's seconds, under 10 I would do an ajax request and have a modal progress message

If they take extended amounts of time my approach would be to create or use an existing task scheduler/ report generator

  • a single system scheduled task entry calling a central management script ( probably not optimal )
  • You mark a task/report for execution
  • Concurrency. Count, limit the number currently executing ( so you don't over load the server)
  • users pool via ajax for their tasks / reports or push to the clients with web sockets

Example on how to fork php to background

Update

I think you would get better performance out of a bot continuously check a database or file for work to do and submitting results back to the database. Alerting users via ajax, web sockets and or email when the work that they need is done / updated.

Here is a good introduction on how to build a web crawler in php

daxroc
  • 316
  • 1
  • 6
  • Actually the script could take something like 5 minutes... But on the other side, the amount of data sent wouldn't be big as it would be smal chunks of text. – Iam Zesh Oct 15 '12 at 08:17
  • Depending on what your doing, PHP might not be a good/best choice for task processing, would be fine for queuing. I think you need to update and clarify on what kind of processing your wanting to do. The more detail the better. – daxroc Oct 15 '12 at 08:20
  • I would like to check if a few (1000+) domains contain some text and it takes quite long for the script to run... – Iam Zesh Oct 15 '12 at 08:29
  • Just curious, Why would your users need to check thousands of domains for text ? – daxroc Oct 15 '12 at 08:34
  • Heres a better intro to a basic bot that looks for content http://www.makeuseof.com/tag/build-basic-web-crawler-pull-information-website/ – daxroc Oct 15 '12 at 08:43