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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Jaxolotl
Forum Contributor
Posts: 137
Joined: Mon Nov 13, 2006 4:19 am
Location: Argentina and Italy

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

Post 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');
 }
?> 
Last edited by Jaxolotl on Thu Mar 26, 2009 11:33 am, edited 1 time in total.
User avatar
Jaxolotl
Forum Contributor
Posts: 137
Joined: Mon Nov 13, 2006 4:19 am
Location: Argentina and Italy

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

Post 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
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

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

Post by pickle »

The location is supposed to be a fully qualified url, not just part of a filename.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
Jaxolotl
Forum Contributor
Posts: 137
Joined: Mon Nov 13, 2006 4:19 am
Location: Argentina and Italy

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

Post by Jaxolotl »

on the script destination1 and destination2 stands for a complete URI
(example: http://www.domainname.tld/filename.ext?GETvars)
User avatar
Apollo
Forum Regular
Posts: 794
Joined: Wed Apr 30, 2008 2:34 am

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

Post 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.
User avatar
Jaxolotl
Forum Contributor
Posts: 137
Joined: Mon Nov 13, 2006 4:19 am
Location: Argentina and Italy

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

Post 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:
Post Reply