1

I am used to headers not being able to be sent after output has started. In fact, I count on it.

In the application I'm working on, after every succesful updating or inserting query, the page is reloaded with a header('Location: ...') call. That's because I don't want the page with the POST data to be refresh-able.

But while developing, one of these queries might just have a bug in them - and then I'm printing out some debug information (with echo). This would then prevent the page from being reloaded - but ever since I upgraded to php 5.4, it simply reloads anyway. I can only read the debug information if I exit; after printing the debug information - which I don't like, because some more debug information might follow later in the script.

If I check headers_sent() in one of those echo calls, it says headers have not been sent yet. So it looks like it's being buffered without me (knowingly) having turned that on.

It might of course be a php.ini setting rather than the mere fact I'm now using version 5.4, but I can't find it.

Someone have an idea?

3
  • Have you tried error_reporting(E_ALL)? Commented Sep 14, 2012 at 14:20
  • 1
    What is the value of output_buffering in your php.ini file? Commented Sep 14, 2012 at 14:41
  • Thank you jeroen - it's silly, but I had looked over this value. output_buffering is indeed set (to 4096) which causes my debug information to be automatically kept in buffer, allowing the header to be set. It must have been off on my previous php installation. Commented Sep 14, 2012 at 16:06

2 Answers 2

1

Use output buffering to solve this. Basically all you need to do is to call ob_start(); as soon as you can. Then, instead of sending stuff out instantly, PHP would buffer the output. So it does not really matter what you set first, body or headers. They will be set in buffer, so manipulation is not a problem unless buffer is sent. If you do not flush buffer explicitly it will be sent by PHP when your script ends. See Output Buffering Control chapter in PHP manual.

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

2 Comments

I understand your suggestion, but I don't think that's what I want: I do NOT want buffering, but I seem to be getting it anyway. I do in fact want the header() call to fail due to the output having been started, so that I can see the debug information.
It was indeed an output buffering issue though - thanks to jeroen's comment on my original question, I was able to find that php was automatically buffering output due to output_buffering being set in php.ini.
1

jeroen answered my question (in a comment on my original question) by pointing me to the output_buffering setting in php.ini. Thank you!

Comments

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.