Categories
PHP Web Development

PHP: Headers cannot be sent or why is headers_sent() incorrect?

Working with PHP, trying to warm up to PHP 5 with all its object-oriented glory, or at least step forward compared to PHP 4.

When you redirect browsers in PHP, you do it raw, without niceties of objects, by sending the HTTP header to the client with a call like:

header(“Location: “);

For some odd reason, this kept on failing, with an error message telling me that the header cannot be sent. Furthermore, PHP even told me through its header ‘detection’ method php headers_sent() that the headers were indeed sent.

Googled and found this article that infers that the issue may have something to do with file encoding. The file that was causing the issues for me was copied from a Windows machine (I am developing on a Linux VM now). It had the Windows line separators (\r\n). Apparently this made Apache and PHP on Linux very very uneasy and made them think that the headers were sent out.

Resolution: Copy the contents of the file to another file that was born on Unix and uses its line separators (\n). Ugly moments in coding.

Share
Share