Cannot modify header information - headers already sent - but my eyes say otherwise

Hi all,

I’m receiving the all too famous “Cannot modify header information - headers already sent by…” error message.

I have browsed the internet for a solution and everywhere suggests this bug is caused by spaces before the “header()” line.

I have tried various versions of modifying the file but it just won’t go away. The code below is right at the top of the sendemail.php file so there is no blank line before it starts:

<?php
if(!isset($_POST[‘name’])) // prevent access to this page unless its through contact.php
{
/* Redirect to a different page in the current directory that was requested */
$host = $_SERVER[‘HTTP_HOST’];
$uri = rtrim(dirname($_SERVER[‘PHP_SELF’]), ‘/\\’);
$extra = ‘contact.php’;
header(“Location: http://$host$uri/$extra”);
exit(); // Needed so if a browser don’t follow redirect headers, the code after is not run
}
?>

After the last ?? is another “<?php” what commences the DOCTYPE/HEADER section of the page. This should only get processed if the above “if(!isset($_POST[‘name’]))” is FALSE.

Do you see any faces because my eyes can’t and I’ve been playing with it for a while now.

I have even tried just making the url it should goto static without all the variables that build the string but to no avail.

Thanks a lot.

The error message tells you the file, and line, where you sent output.

Yes but I kept looking there and nothing was being output.

I’ve just had an idea to save thefile as UTF-8 without BOM and guess what? it FINALLY works. Some bytes were obviously sent that were getting mixed up with the PHP syntax.

You had a space before your <?php tag…

Actually in this case it is the BOM…
PHP has a nasty bug that it doesn’t recognize the BOM and it outputs it.
I spend 5 days finding a bug in my code which resulted in finally finding out that it was the BOM

This bug us known to the PHP developers and should be solved in PHP6.
It’s somewhere in the buglist.

Same goes for file_get_contents, it also spits out the BOM instead of ignoring it.

The only way around it at this point is to use UTF8 without BOM

Really nasty bug in my eyes…

the bug report:
http://bugs.php.net/bug.php?id=22108

Not in the file, in the file format it was saved as. Going from UTF-8 to UTF-8 Without BOM fixed it.

In regards to the file_get_contents BOM issue I use the following to solve it:


$str = file_get_contents($filePath);        
/* file_get_contents doesn't strip the utf BOM away, so we need to do it ourselfs */
if (substr($str, 0, 3) == pack('CCC', 0xEF, 0xBB, 0xBF)) $str = substr($str, 3);

This enables you to read files with the BOM correctly, although it doesn’t solve the problem where your php script itself has a BOM. No solution to that.

also you can avoid this by surrounding ur code with


ob_start();
... header(...);
header();
echo
print
ob_end_flush();

no, you can’t
read topic before answer

Thank you for your help on this guys. I appreciate it.

That’s what I meant, it usually happens to me when I copy code/files from one format to another…

Next time it happens, you will know about it.

Ex:

  • code looks fine, but won’t work.
  • create a new file, copy paste the code from the old file in the new one.
  • save, rename, and go on with your codding. :smiley:

Yes, thanks. It’s always the first time that takes the longest. After that you remember how to go about fixing the bugs that come back and haunt you sooner or later :slight_smile:

[ot]

What has fishing got to do with it Vali?!:p[/ot]