13

Title is self-explanatory.

I have a good bit of experience with PHP, but I am not sure how the header function works between ob_start() and ob_end_clean().

Consider this:

ob_start();

echo "Some content";
header('X-Example-Header: foo');
echo "Some more content";

$output = ob_get_contents();
ob_end_clean();

echo $output;

Does the header function ignore the output buffering, and thus all headers get sent before the content because it is echoed after the header call?

Or does it work some other way?

2
  • 3
    (reference) ob_start — This function will turn output buffering on. While output buffering is active no output is sent from the script (other than headers), instead the output is stored in an internal buffer. Commented Jun 24, 2010 at 17:56
  • Oh, hey, I missed that. That should teach me to read a bit closer. Thanks, Gordon. Commented Jun 24, 2010 at 20:18

1 Answer 1

20

The header() does indeed ignore output buffering. Part of the reason to use output buffering is so you can send HTTP headers "out of order" since the response is buffered. You can't send HTTP headers once you've sent any kind of output (unless that output is buffered).

Sign up to request clarification or add additional context in comments.

1 Comment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.