header function fails even on pure php pages

geirgrusom

New Member
Messages
9
Reaction score
0
Points
0
Suddenly, today (or at least today I noticed it), the header functions always returns

Warning: Cannot modify header information - headers already sent by (output started at /home/me/somefile_php:1) in /home/me/some_file.php on line x

Why has this suddenly started? the page in question is a pure PHP page, and there is no HTML generated by it. It is just supposed to redirect to the source page after data has been recorded in the database.

Also, this renders my captcha system useless which encounters the same error when trying to call
header("Content-type: image/png");

Anyone had a similar problem?
 

lemon-tree

x10 Minion
Community Support
Messages
1,420
Reaction score
46
Points
48
Are you sure it is not trying to send an output to the browser?
 

blobtech

New Member
Prime Account
Messages
17
Reaction score
0
Points
1
Its probally because a empty line or a space just infront of the <?php tag.

Make sure there is absolutely *no* text nor whitespace which is not included within a php tag before the header() functions is called.

Also check any included files, they might contain that one byte that is messing it up!
 
Last edited:

garrettroyce

Community Support
Community Support
Messages
5,604
Reaction score
245
Points
63
blobtech's answer fixes about 99.9% of these occurrences. If you intentionally want to have output before your PHP code you can use ob_start() and ob_flush() functions. But, ob_start() has to be called before any output to the browser.

example:
Code:
<?php ob_start(); ?>
<!DOCTYPE ...
<?php //OH NO! I changed my mind! I want to redirect!
header("Location: ...");
ob_flush();

Anything that has been buffered by the ob_start() function, will be overwritten if you ask it to before you call ob_flush().
 
Last edited:

geirgrusom

New Member
Messages
9
Reaction score
0
Points
0
Thanks for replying :)
But there really is nothing before <?php
Not a single space or line break, at least thats what my text editor tells me.
Could it be related to the UTF-8 BOM character?

edit: turned out it was the BOM character, Thanks for replying :)
 
Last edited:

geirgrusom

New Member
Messages
9
Reaction score
0
Points
0
I use Visual Studio, so I fixed it by choosing Advanced Save Options->Use UTF8 without byte order mark.
 

taekwondokid42

New Member
Messages
268
Reaction score
0
Points
0
I think your problem is that you are sending output to the page, and then you are trying to modify the headers (set a cookie, start a session, ect.).

This is illegal code:


<?

echo "hello";
setcookie(...);

?>


Instead, this will not be illegal:

<?

setcookie(...);
echo

?>
 

dickey

New Member
Messages
128
Reaction score
0
Points
0
I'll go with smooth2 it is your Character encoding that is the problem...

try to match your php file's encoding with it's with the head declaration.
 
Top