views:

1096

answers:

9

Is it possible to redirect a user to a different page through the user of PHP?

Say the user goes to www.example.com/page.php and it I want to redirect them to www.example.com/index.php , how would I do so without the use of a meta refresh? Possible?

  • This could even protect my pages from unauthorized users. :]

Please help

+2  A: 
vartec
Documentation? :)
Paolo Bergantino
die() or exit() is for clients who don't respect the "Location: ..." header
TenebrousX
+2  A: 

you can update the header in php: header

Zack
A: 

header( 'Location: http://www.yoursite.com/new_page.html' );

Daniel A. White
+15  A: 
function Redirect($url, $permanent = false)
{
    if (headers_sent() === false)
    {
     header('Location: ' . $url, true, ($permanent === true) ? 301 : 302);
    }

    exit();
}

Redirect('http://www.google.com/', false);

Don't forget to die()/exit()!

Alix Axel
And don't forget output buffering or you'll end up with 'Headers already sent'.
Kuroki Kaze
... and dont forget th print out somthign like "you'll be redirected to $nepage in $n seconds, click $link here if redirect dont happen"Some broser, and some browser's settings, may fail that redirect.
DaNieL
@DaNieL: this type of redirect won't take "$n seconds". It will be instant if it happens at all, and any conforming browser should handle it. I think you're thinking of the "meta refresh" redirects that people use when they don't know any better.
rmeador
@rmeador... For older browsers and speciality browsers. You should first do your Location header, if fails have a meta-redirect with the "you'll be redirected to page in x seconds" with a link in case the meta-redirect fails. That's the proper and fail-safe way of doing a redirect.
Andrew Moore
which browsers are these?
nickf
Andrew: how can HTTP browser not respect Location:?
vartec
does not incorporate the 303 status.
tharkun
@Kuroki Kaze: You won't end up with 'Headers already sent' because of the headers_sent() check.
Alix Axel
+25  A: 

Summary of existing answers plus my own two cents:

1. Basic answer

You can use the header() function to send a new HTTP header.

header('Location: '.$newURL);

2. Important details

die()

header("Location: myOtherPage.php");
die();

Why you should use die(): The Daily WTF

Absolute URL

The URL must be an absolute. See RFC 2616. But in most cases a relative URL will be accepted too.

Status Codes

PHP's "Location"-header still uses the HTTP 302-redirect code. The one it should use instead, is the 303 one.

W3C mentions that the 303-header is incompatible with "many pre-HTTP/1.1 user agents.

Currently used browsers are all HTTP/1.1 user agents. This is not true for many other user agents like spiders and robots.

3. Documentation

HTTP Headers and the header() function in PHP

4. Alternatives

You may use the alternative method of http_redirect($url); which needs the PECL package pecl to be installed.

5. Helper Functions

This function doesn't incorporate the 303 status code:

function Redirect($url, $permanent = false)
{
    header('Location: ' . $url, true, $permanent ? 301 : 302);

    exit();
}

Redirect('http://www.google.com/', false);

This is more flexible:

function redirect($url, $statusCode = 303)
{
   header('Location: ' . $url, true, $statusCode);
   die();
}
tharkun
+1  A: 

Like others here said, sending the location header with:

header( "Location: http://www.mywebsite.com/otherpage.php" );

but you need to do it before you've sent any other output to the browser.

Also, if you're going to use this to block un-authenticated users from certain pages, like you mentioned, keep in mind that some user agents will ignore this and continue on the current page anyway, so you'll need to die() after you send it.

Brent
+5  A: 

Most of these answers are forgetting a very important step!

header("Location: myOtherPage.php");
die();

Leaving that vital second line out might see you end up on The Daily WTF. The problem is that browsers do not have to respect the headers which your page return, so with headers being ignored, the rest of the page will be executed without a redirect.

nickf
If it's so important you may want to consider explaining why
Joe Philllips
fair call.. editing now.
nickf
What's about give some output to the user before kill the script?You know, people love to know what is happenin...
DaNieL
you're assuming that the script has nothing to do except redirect. Which might be not true at all.
vartec
@DaNieL: change it to die("Stop ignoring my headers!")
nickf
A: 

Use the header() function to manipulate the HTTP header and add the Location header field with the absolute URL you want to redirect to:

header('Location: http://www.example.com/index.php');


Edit   The URL must be an absolute. See RFC 2616:

Location       = "Location" ":" absoluteURI

Though most user agents accept relative URLs as well.

Gumbo
I think the url doesn't have to be absolute.
lalala2007
+1  A: 

In the eve of the semantic web, correctness is something to consider. Unfortunately, PHP's "Location"-header still uses the HTTP 302-redirect code, which, strictly, isn't the best one for redirection. The one it should use instead, is the 303 one.

W3C is kind enough to mention that the 303-header is incompatible with "many pre-HTTP/1.1 user agents," which would amount to no browser in current use. So, the 302 is a relic, which shouldn't be used.

...or you could just ignore it, as everyone else...

Henrik Paul