php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #32512 flush() does not work after sending Location header
Submitted: 2005-03-31 10:32 UTC Modified: 2005-04-04 10:45 UTC
From: kulakov74 at yandex dot ru Assigned:
Status: Not a bug Package: Output Control
PHP Version: 4.3.9 OS: Linux, Win 2000
Private report: No CVE-ID: None
 [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. 

Patches

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-03-31 22:00 UTC] sniper@php.net
Please try using this CVS snapshot:

  http://snaps.php.net/php4-STABLE-latest.tar.gz
 
For Windows:
 
  http://snaps.php.net/win32/php4-win32-STABLE-latest.zip

I can NOT reproduce this with latest CVS (tested PHP_4_3 / HEAD branches)
 [2005-04-01 09:53 UTC] kulakov74 at yandex dot ru
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.
 [2005-04-01 10:40 UTC] sniper@php.net
But no bug in flush...

 [2005-04-04 10:45 UTC] kulakov74 at yandex dot ru
Yes, sorry again - it was my fault and the bug should be closed / deleted. As for register_shutdown_function(), I checked the updated documentation and found I was wrong too - connection should be opened.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Aug 13 23:01:28 2024 UTC