Forum Moderators: coopster

Message Too Old, No Replies

php header error even when no prior output

         

jlake

11:51 am on Aug 12, 2009 (gmt 0)

10+ Year Member



I confess myself completely lost. I'm running a very simple user authentication script where I'm trying to set a cookie. The server is responding with a header error, even though there hasn't been any ACTUAL output prior to setting the cookie (there's ECHO output in conditional statements where the conditions are NOT met, but no actual output).

But, no matter... while messing about trying to fix it, I finally just wrote a simple php file:


<?php
setcookie("blahdiblah", "blah");
?>

... and I got the same error:

Cannot modify header information - headers already sent

I have confirmed in both files that there is no whitespace prior to the php start tag, nor any after the end tag.

The php file is UTF8 encoded, in case anyone's aware of weird things happening with whitespace in UTF8, although I've used UTF8 for php plenty of times before with no issue. I'm running the script on an apache 2 web server.

Can anyone tell me what the heck is going on?!?! I'll just be patiently awaiting a response while I bang my head repeatedly against a wall. :)

~j.

SmallPotatoes

3:36 pm on Aug 12, 2009 (gmt 0)

10+ Year Member



Your PHP source file almost certainly contains the Unicode Byte Order Mark (U+FEFF) at the beginning. Tell your text editor to save without it.

If you're using BBEdit, you'd choose "Unicode (UTF-8, no BOM)"; other text editors may have a different way of labeling the appropriate format.

vincevincevince

3:51 pm on Aug 12, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



A dirty fix is to set output_buffering to on using php.ini or .htaccess - this will fix the problem.

jlake

8:43 pm on Aug 12, 2009 (gmt 0)

10+ Year Member



Thank-you both for your help. When I saved the file in Unicode, the server inexplicably stopped processing it as a php script. So I made the changes to the output_buffering setting (which I realize is less than ideal, but it saves me from tearing out what little hair I have left, for the time being). The latter change solved the problem. Thank-you both again for your help.

SmallPotatoes

8:52 pm on Aug 12, 2009 (gmt 0)

10+ Year Member



You can't just save it as Unicode, it has to be Unicode-UTF8-with-no-BOM. There are myriad different ways to express Unicode in a raw bytestream, each with its own ramifications. If your text-editor program doesn't have that level of specificity then it is worthless and its author's home should be torched by angry villagers.