1

I wrote

<?
header("Location:http://example.com");
?>

but Redirect is not occured. How to redirect?

But I do not have authority to edit php.ini So safe_mode is on in php.ini

freddiefujiwara
  • 57,041
  • 28
  • 76
  • 106

6 Answers6

8

Try:

header("Location: http://example.com");

HTTP headers need to exactly follow the spec. More directly here (Location header):

http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.30

karim79
  • 339,989
  • 67
  • 413
  • 406
  • 2
    also, use in the end, causes erros sometimes. – yoda Dec 03 '09 at 02:04
  • 2
    and `die()` or `exit()` right after or else the rest of the script will parse wasting cpu/memory on stuff the user will never see. – Mike B Dec 03 '09 at 02:13
  • @yoda - true, the script will fail in potentially horrible ways if short tags are not enabled by configuration, and the same applies to whitespace after the final closing tag. – karim79 Dec 03 '09 at 02:13
4

One possible issue is that there was something got "printed out" before you issue the above code. So check your code so that there is nothing got "echoed" before reached this line.

Michael Mao
  • 9,878
  • 23
  • 75
  • 91
  • 1
    pay attention to white space outside of `` tags too. If an included file has whitespace at its end and it's called before `header()` the redirect will fail. – dnagirl Dec 03 '09 at 02:48
  • dnagirl makes a good point. The header() function should be among the very first lines, not messed up with other included files or echo statements. Yeah, die() after you issue the header() function, to be safe. Just by the way, if you cannot walk around this issue, you can try using javascript to do it anyway :) – Michael Mao Dec 03 '09 at 03:19
2

Two things:

  • You have to make sure you haven't sent any other HTML before sending your header.
  • You should also exit or die() after your header() call.

See this post for more detailed information.

You can also use JavaScript to do the redirect but I suspect PHP is probably a better idea in your situation.

Community
  • 1
  • 1
Chris Williams
  • 11,647
  • 15
  • 60
  • 97
1

Make sure you alway add die() after the header() call. This is extremely important if anything is output below the header() that the user is not supposed to see.

Pekka
  • 442,112
  • 142
  • 972
  • 1,088
0

Make sure you have nothing prior to the opening "

If that still doesn't work, are you getting any sort of error message?

Brian Showalter
  • 4,321
  • 2
  • 26
  • 29
0

Alternatively, use:

<meta http-equiv="refresh" content="0;url=http://foo.com">

somewhere in your <head> section.

Source.

Conrad Meyer
  • 2,851
  • 21
  • 24
  • 5
    Not a good practice, meta refresh has been widely used in spam and porn sites and for that could harm your site in search engines. – MaxiWheat Dec 03 '09 at 02:20
  • Good point Conrad, I didn't know that before. So the two mainstream ways of doing redirect are: PHP redirect and javascript redirect, right? I prefer PHP redirect myself :) – Michael Mao Dec 03 '09 at 03:22
  • @MaciWheat: *Shrug*. At least in my experience with PHP, you don't always have the ability to send headers (e.g. on some free webhosting sites "back in the day", they'd send a banner before your php ran). @Michael: It's not Javascript. It works for users with javascript disabled. – Conrad Meyer Dec 06 '09 at 08:25