PHP: Redirection Header Before Background Processes Complete

Using PHP, how can I send a redirect header first, then do something in the background after the browser has already be redirected?  

In the example below, even though the redirection is called for immediately, there is a 30 second pause before it occurs.
<?php
ignore_user_abort(true);
set_time_limit(0);
header('Location: http://www.example.com/');

sleep(30);

// do Something in the background

exit;
?>

Open in new window

LVL 10
skijAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mark BradyPrincipal Data EngineerCommented:
Each line of code is evaluated one at a time. Your sleep(30) line will never be evaluated because the browser has already been redirected causing execution of that page to stop.

On the other hand, if you do something in the background before calling the redirect then the redirect will not occur until the background
<?php // test.php

// do some stuff here then...

$external_file = 'tasks.php';
exec('php tasks.php > /dev/null 2>&1 &'); // you can add arguments to this line if you need to pass them to the script

// forget the 30 second sleep because the above line will execute immediately and start processing the next line of code which should be your redirect.
header('Location: http://www.example.com/');

?>

Open in new window

code has finished.

What I would do is whatever the tasks you need to run in the background, put that code in a different php file then you execute that file first, then do the redirect.

Something like this....
Dave BaldwinFixer of ProblemsCommented:
You can't do that in PHP, at least not with the code you posted.  The 'header' is something the web server sends to the browser.  And the web server will wait until the PHP script is done before it sends anything in your code because there is no content.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ray PaseurCommented:
Using PHP, how can I send a redirect header first, then do something in the background after the browser has already be redirected?  
This is an undependable strategy.  The header() function will send a command to the browser, then the script will keep right on running.  Exactly when the browser redirects -- that is somewhat unpredictable.  It will probably send a "stop" to the server, but that is not certain.

The correct way to do this is to start an asynchronous process.  This can be started with fsockopen() or a cURL POST request.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.