|   | php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login | 
| 
  [2005-03-31 10:32 UTC] kulakov74 at yandex dot ru
 Description:
------------
We want a script to make a redirect and then make some Sql-queries, so that the user would not wait for the queries to execute (sometimes they may take too long). I added 
echo("-"); flush();
after sending the Location header which made PHP send the header immediately away, but the problem is IE does not make the redirect as soon as it gets the header - probably it expects other headers or a page, so it only redirects after the script completes. If I add more output after that and a flush() call then PHP won't output anything else until the script completes. This is emulated with a sleep(3) call. More precisely, PHP only sends headers immediately, it doesn't send anything else (the dash in this case). 
Reproduce code:
---------------
//This is the redirect
header("Location: http://hotelsys.biz/hotels/Bali");
//This is how I force PHP to send it right away
echo("-"); flush();
//This is how I cannot make PHP send anything else
echo(str_repeat("-", 1024*16)); flush();
//Pause emulation
sleep(3);
//the end - this is when the browser gets the output
exit;
Expected result:
----------------
The first "-" character and the next 16K of it sent right away. 
Actual result:
--------------
I only get dashes in 3 seconds; the browser (IE) makes the redirect in this time too. 
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits             | |||||||||||||||||||||||||||
|  Copyright © 2001-2025 The PHP Group All rights reserved. | Last updated: Fri Oct 31 03:00:01 2025 UTC | 
I'm sorry, this is my fault - the HTTP-interface I use for testing could not display single characters which made me think PHP didn't send anything. After I added newlines to the output - echo("-\n"); flush(); - it worked as expected. Also, I tried using register_shutdown_function to separate the slow part from sending headers, but it didn't work. The docs says: The registered shutdown functions are called after the request has been completed (including sending any output buffers), so it is not possible to send output to the browser using echo() or print()... But I tried to output with echo() within the regisreted function and, to my surprise, the output got to the browser! Seems like when the function is called the connection may well be open unless it was closed by the user/net; I wish there would be a way to tell Apache to close it at any moment so that script would run in background.