-1

I need to echo some output while executing php file ,beacuse execution takes 10 sec and end of 10sec page should be directed via header("Location:test.php)

However If I use ob_start and ob_implicit_flush(true) at the same time , we cannot direct page and getting

Warning: Cannot modify header information - headers already sent by

I also need to use ob_implicit_flush(true) to print output while execution.

How can I display output and direct page ?

2 Answers2

0

You cannot output both body content and a redirect header in the same response, much less output the body before the header. The HTTP headers come first, so you cannot output a body before headers and you cannot output headers after the body has been output. Further, a redirect header causes the request to be immediately redirected before the browser will display any of its content, so the entire thing doesn't work on two levels.

If you want to display anything while the server is doing something, you'll need to use Javascript in some form or another.

deceze
  • 510,633
  • 85
  • 743
  • 889
0

If you use header() function there MUST be no output BEFORE it. But of course it can be after using the function.

ob_start starts the buffer to work while ob_implicit_flush tells to use no buffer at all. So the two functions cannot be combined that way.

Example:

ob_start(); //set buffer on
print('Hello World'); //no output since buffer on
ob_implicit_flush(true); //buffer switched off again
print('Test'); //prints 'Hello World' and 'Test'
header('Location: ...'); //ERROR: output already done

You have now to decide if you want to output information from your script OR make the redirection.
Output something AFTER header is possible but it makes no sense as you will not see it anymore.

Maybe you can use the ob_get_length() function to check if there was some output and then decide if you switch page or output the buffer:

ob_start(); //set buffer on
print('Hello World'); //no output since buffer on
if(ob_get_length() > 0)
  ob_end_flush();
else
  header('Location: ...'); //will not be executed if output was generated
Klaus F.
  • 135
  • 14