How to make a redirect in PHP?

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

Say the user goes to www.example.com/page.php and 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.


Summary of existing answers plus my own two cents:

1. Basic answer

You can use the header() function to send a new HTTP header, but this must be sent to the browser before any HTML or text (so before the <!DOCTYPE ...> declaration, for example).

header('Location: '.$newURL);

2. Important details

die() or exit()

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

Why you should use die() or exit() : 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, but this is not the one you should use. You should consider either 301 (permanent redirect) or 303 (other).

Note: 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

  • What the PHP manual says
  • What Wikipedia says
  • What the W3C says
  • 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://example.com/', false);
    

    This is more flexible:

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

    6. Workaround

    As mentioned header() redirects only work before anything is written out. They usually fail if invoked inmidst HTML output. Then you might use a HTML header workaround (not very professional!) like:

     <meta http-equiv="refresh" content="0;url=finalpage.html">
    

    Or a JavaScript redirect even.

    window.location.replace("http://example.com/");
    

    function Redirect($url, $permanent = false)
    {
        if (headers_sent() === false)
        {
            header('Location: ' . $url, true, ($permanent === true) ? 301 : 302);
        }
    
        exit();
    }
    
    Redirect('http://www.google.com/', false);
    

    不要忘了死()/退出()!


    Output JavaScript from PHP using echo, which will do the job.

    echo '<script type="text/javascript">
               window.location = "http://www.google.com/"
          </script>';
    

    You can't really do it in PHP unless you buffer the page output and then later check for redirect condition. That might be too much of a hassle. Remember that headers are the first thing that is sent from the page. Most of the redirect is usually required later in the page. For that you have to buffer all the output of the page and check for redirect condition later. At that point you can either redirect page user header() or simply echo the buffered output.

    For more about buffering (advantages)

    What is output buffering?

    链接地址: http://www.djcxy.com/p/892.html

    上一篇: 如何使用JavaScript重定向?

    下一篇: 如何在PHP中进行重定向?