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.
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.
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
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.
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