tags:

views:

109

answers:

6

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

+6  A: 

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
also, use <?php instead of <?, and avoid using ?> in the end, causes erros sometimes.
yoda
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
@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
+4  A: 

Hi

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
pay attention to white space outside of `<?php ?>` tags too. If an included file has whitespace at its end and it's called before `header()` the redirect will fail.
dnagirl
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
+1  A: 

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.

Chris Williams
+1  A: 

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
A: 
Brian Showalter
A: 

Alternatively, use:

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

somewhere in your <head> section.

Source.

Conrad Meyer
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
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
@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