the headers already sent error refers to output being written before certain headers (e.g. header(), cookie()) are sent.
Ensure that output is only written after such functions are called, or if you really cannot avoid it, use output buffering.
As to why it works in one place and does not in another, I can only guess that different PHP versions, configurations, etc, could have caused the script control to follow differing paths.