-2

So the weirdest thing is happening and I'm honestly stumped. I don't think I need to post a code example, and I'll tell you why.

I'm running a PHP header("location:blah.php") on my localhost and it works fine. When I migrate everything to my 1and1 hosting account, the header tags will not work at all. This obviously has nothing to do with the source.

Any ideas why it wont work when I put it on my other host?

  • 2
    error/notices ?? checked any?? – swapnesh Apr 07 '13 at 10:10
  • The obvious reason would be that something is outputted before header() executes. Either an empty character before the PHP tags, byte order mark or something like that ? – adeneo Apr 07 '13 at 10:10
  • “This obviously has nothing to do with the source.” – Are you sure? Often `header` fails because there was already some output before that call. You should check that. – Gumbo Apr 07 '13 at 10:10
  • Please check if there is any output before header function. Even a space can create problem. Make it a habit to turn on error reporting. – Harpreet Apr 07 '13 at 10:11
  • If you call something like `echo` before `header`, it will give you an error like that. – emma.fn2 Apr 07 '13 at 10:11
  • I understand that if something is called before the header it will not work, but this is working on one host and not the other.... Same exact code. – Christian Rankin Apr 07 '13 at 10:14
  • Error reporting is turned on and there is nothing. Also, I'm not a noob at PHP, this is just a strange occurrence. – Christian Rankin Apr 07 '13 at 10:15
  • Even 1 blank space before the PHP tag may be a problem... Look carefull at your code for any possible problem – Svetoslav Apr 07 '13 at 10:16
  • Check it with vi. You will see some strange characters that your powerful IDE can't display. – AKS Apr 07 '13 at 10:18
  • @Svetlio, I stipped all the spaces just to be sure, and still doesn't work. – Christian Rankin Apr 07 '13 at 10:20
  • Only reason I said it has nothing to do with the source, was because it was working on one host, and that didn't seem like a source issue. – Christian Rankin Apr 07 '13 at 10:30

2 Answers2

3

Try with an absolute URI, instead of a relative one:

header("Location: http://yourhost.com/blah.php");

You may refer to http://php.net/manual/en/function.header.php It says:

HTTP/1.1 requires an absolute URI as argument to » Location: including the scheme, hostname and absolute path, but some clients accept relative URIs. You can usually use $_SERVER['HTTP_HOST'], $_SERVER['PHP_SELF'] and dirname() to make an absolute URI from a relative one yourself

By the way, make sure you're not sending any output before the header() call. Not even a blank line.

Ander Juaristi
  • 328
  • 3
  • 12
2
error_reporting(E_ALL);
if(!headers_sent()){
    header("location:blah.php");
    exit("Headers aren't set");
} else{
    exit("Headers already sent");
}

Try that.. If headers are sent you will get second mesasge. If headers aren't sent you must be redirect if you are not you will get the first message.

In case they are sent you have to find where exactly is that.. Normally php error reporting tells you that.

Svetoslav
  • 4,686
  • 2
  • 28
  • 43