Page 1 of 1

[SOLVED]header() doesn't redirect, no headers sent before

Posted: Thu Mar 26, 2009 7:55 am
by Jaxolotl
Does anyone knows why header('Location:') doesn't redirect?
No headers has been sent before, no white space befor starting php code.
The problem is verified in only two of all servers i've ever use.
Thanks in advance to everyone.
the code is exactly this one.

Code: Select all

 <?php
 if(CONDITION){
      $_SESSION['SOMETHING'] = "SESSION VALUE";
      header('Location: destination1');
}
elseif(CONDITION){
            echo "SOMETHING";
}
else{
      header('Location: destination2');
 }
?> 

Re: header('Location:') doesn't redirect, no headers sent before

Posted: Thu Mar 26, 2009 8:18 am
by Jaxolotl
The problem occurs only when the file encoding is UTF-8 ; when is UTF-8 NO BOM it doesn't happens.

Anyone knows the reason? please apologise my ignorance

Re: header('Location:') doesn't redirect, no headers sent before

Posted: Thu Mar 26, 2009 10:39 am
by pickle
The location is supposed to be a fully qualified url, not just part of a filename.

Re: header('Location:') doesn't redirect, no headers sent before

Posted: Thu Mar 26, 2009 10:58 am
by Jaxolotl
on the script destination1 and destination2 stands for a complete URI
(example: http://www.domainname.tld/filename.ext?GETvars)

Re: header('Location:') doesn't redirect, no headers sent before

Posted: Thu Mar 26, 2009 11:05 am
by Apollo
Jaxolotl wrote:The problem occurs only when the file encoding is UTF-8 ; when is UTF-8 NO BOM it doesn't happens.
You mean the encoding of the .php file? That should NEVER contain a UTF-8 BOM.

What happens is if your php file as a utf-8 bom, it will be in the file before the <?php tag. And that is outputted as straight html, before the php parser kicks in. And if there's already some html output, you can't send headers anymore.

Re: header('Location:') doesn't redirect, no headers sent before

Posted: Thu Mar 26, 2009 11:32 am
by Jaxolotl
Apollo wrote: You mean the encoding of the .php file? That should NEVER contain a UTF-8 BOM.

What happens is if your php file as a utf-8 bom, it will be in the file before the <?php tag. And that is outputted as straight html, before the php parser kicks in. And if there's already some html output, you can't send headers anymore.
Thats it!!! Thnx Apollo! Shame on me :oops: