My page executes a script that takes a relatively long time to complete. I would like to make it so that the user can submit information, immediately echo "Complete", and allow the user to exit the page while the script continues executing. How can I do this?
-
1You need to tell which SAPI you're using, e.g. FCGI / PHP FPM allows you to do that with a simple function call, see [example of how to use fastcgi_finish_request()](http://stackoverflow.com/questions/4236040/example-of-how-to-use-fastcgi-finish-request). – hakre Oct 15 '12 at 23:41
-
@hakre Im running apache not nginx – CJ Sculti Oct 16 '12 at 00:05
-
@hakre I tried to install PHP-FPM but Cpanel fricken has my server locked down. It hijacks all the config files, everything -_-. Are there any equivelents that would work? – CJ Sculti Oct 16 '12 at 00:43
-
I bet, but still you have not told which SAPI you're using. I'd say you should improve your question first. – hakre Oct 16 '12 at 07:57
-
@hakre I am using CGI PHP. Accirding to the script here: http://php.net/manual/en/function.php-sapi-name.php – CJ Sculti Oct 16 '12 at 19:00
-
@hakre PHP 5.3.17 (cli) (built: Oct 14 2012 23:07:16) – CJ Sculti Oct 16 '12 at 22:29
3 Answers
Use cron. On your page create a email task and save in in db or fs - does not matter. Create a script which runs every n minutes which gets email tasks and executes them.
Unfortunately, your hosting may not have cron support...

- 10,994
- 2
- 38
- 44
Email is naturally slow. I would advise you to use a job queue for your emails. You should look at
With these solutions, you can queue slow tasks and continue to show valid information and progress to your user.
Example Client
$client= new GearmanClient();
$client->addServer();
$client->do("email", json_encode(array("A@yahoo.com","Hello World","This is your first Mail")));
echo "Welcome To XYZ" ;
Server
$worker = new GearmanWorker();
$worker->addServer();
$worker->addFunction("email", "sendMail");
while ( $worker->work() );
function sendMail($mail) {
list($to, $subject, $message) = json_decode($mail);
return mail($to, $subject, $message);
}

- 94,024
- 28
- 166
- 217
-
-
That is easy .. you remove the mail function and add Swift as soon as you have Gearman configured – Baba Oct 16 '12 at 00:06
My suggestion would be to submit all of the information into a table row or similar data structure, then run a cronjob every few minutes to go through each row and run the script based on the information that had been submitted.
This would be slightly complicated, I'm afraid, but it would immediately free the user (once the raw information was stored into the DB)

- 10,042
- 11
- 48
- 64