If you don't flush some output after setting the header() then the
headers won't go out until the end of the request. So do something like:
ignore_user_abort(true);
header("Location: http://whatever");
echo "foo\n"; flush();
Then whatever comes after this should run and the browser is long gone.
-Rasmus
Liang ZHONG wrote:
> I think I did not express myself clearly.
>
> What I want is to be able to redirect user an existing page (let them
> get it immediately), and to close the connection actively, NOT passively
> by user abort, at last, to run the function in background.
>
> But the redirecting using function header() with location has a problem
> that header() always does return the page to user after the entire
> program, including registered showdown function finish running, which is
> against the will. I put a time consuming task into a function that
> registered to be a shutdown function and hoping it runs after the user
> has got the redirected page and the connection has been closed. But
> experiements (using browsers, curl command line tool as well as
> perl::LWP code) show that the user got the redirected page only after
> the shutdown function finished, which is against the description of
> register_shutdown_function at php website.
>
> It seems only header() function use to redirect page has this problem
> (not executed until register_shutdown_function finished) while other
> functions like print()/echo(), exec() have not.
>
> The code looks like:
> ---------------------------------------------------------------------------------------------
>
> <?php
> set_time_limit(1);
>
> function f(){
> set_time_limit(20);
> $count=50000000;
> for($i=0; $i<$count; $i++){ }
> echo "end";
> exec("touch /home/.nappy/liang/liang.ns2user.info/php/aaa");
> }
>
> register_shutdown_function('f');
>
> header("Content-type: text/plain");
> header("Location: y.html");
> ?>
> ---------------------------------------------------------------------------------------------
>
>
> http client who sends the request to the php program will only get the
> page back as response after function f finsihes (file aaa created).
> Changing the $count will make a lot difference.
>
> My BIGGEST question is:
> How to make user get the redirect page immediately after the header() is
> called, and not until function f() ends, while making sure that the
> function f() will finally fully (and might slowly) execute?
>
> Thank you very much for kindly replying.
>
> With high respect,
>
> Liang
>
>
>>
>> Liang ZHONG wrote:
>> > My Question is:
>> > What is the correct way to keep the function running after I
>> redirect an
>> > existing page to http client (which I want the client get immediately)
>> > and then immediately close the connection?
>>
>> ignore_user_abort(true);
>>
>> -Rasmus
>>
>> --
>> PHP General Mailing List (http://www.php.net/)
>> To unsubscribe, visit: http://www.php.net/unsub.php
>>
>
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php