How to make a redirect in PHP?

2018-12-30 22:07发布

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.

标签: php redirect
28条回答
弹指情弦暗扣
2楼-- · 2018-12-30 22:38

you can use some java script methods like below

 1)self.location="http://www.example.com/index.php";

 2)window.location.href="http://www.example.com/index.php";

 3)document.location.href = 'http://www.example.com/index.php';  

 4)window.location.replace("http://www.example.com/index.php");
查看更多
一个人的天荒地老
3楼-- · 2018-12-30 22:40
header("Location: /index.php");
exit(0);   
查看更多
妖精总统
4楼-- · 2018-12-30 22:40

Probably too late to answer this one. Nevertheless, here are my thoughts:

IMHO, the best way to re-direct an incoming request would be by using location headers, which goes

<?php
header("Location: /index.php");
?>

Once this statement is executed, and output sent out, the browser will begin re-directing the user. However, ensure that there hasn't been any output (any echo / var_dump) before sending headers, else it will lead to errors.

Although this is a quick and dirty way to achieve what was originally asked, yet it would eventually turn out to be an SEO disaster, as this kind of re-direct is always interpreted as a 301 / 302 re-direct, hence search engines will always see your index page as a re-directed page, and not something of a landing page / main page. Hence it will affect the SEO settings of the website.

查看更多
浅入江南
5楼-- · 2018-12-30 22:42

Yes, you can use header() function,

header("Location: http://www.yourwebsite.com/user.php"); /* Redirect browser */
exit();

And also best practice is to call exit() function right after header() function to avoid below code execution.

According to the documentation, header() must be called before any actual output is sent.

查看更多
萌妹纸的霸气范
6楼-- · 2018-12-30 22:44

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 or relative URL

Since June 2014 both absolute and relative URLs can be used. See RFC 7231 which had replaced the old RFC 2616, where only absolute URLs were allowed.

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

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/");
查看更多
低头抚发
7楼-- · 2018-12-30 22:44

The best way to Redirect with PHP is the following code...

 header("Location: /index.php");

Make sure no code will work after

header("Location: /index.php");

All the codes must be executed before the above line.

Suppose,

Case 1:

echo "I am a web developer";
header("Location: /index.php");

It will redirect properly to the location (index.php).

Case 2:

return $something;
header("Location: /index.php");

The above code will not redirect to the location(index.php).

Hopefully, It is clear.

查看更多
登录 后发表回答