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

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.

查看更多
君临天下
3楼-- · 2018-12-30 22:47

To redirect the visitor to another page (particularly useful in a conditional loop), simply use the following code:

<?php 
header('Location: mypage.php'); 
?>

In this case, mypage.php is the address of the page to which you would like to redirect the visitors. This address can be absolute and may also include the parameters in this format: mypage.php?param1=val1¶m2=val2)

Relative/Absolute Path

When dealing with relative or absolute paths, it is ideal to choose an absolute path from the root of the server (DOCUMENT_ROOT). Use the following format:

<?php 
header('Location: /directory/mypage.php'); 
?>

If ever the target page is on another server, you include the full URL:

<?php 
header('Location: http://www.ccm.net/forum/'); 
?> 

HTTP Headers

According to HTTP protocol, HTTP headers must be sent before any type of content. This means that no characters should ever be sent before the header — not even an empty space!

Temporary/Permanent Redirections

By default, the type of redirection presented above is a temporary one. This means that search engines, such as Google, will not take the redirection into account when indexing.

If you would like to notify search engines that a page has been permanently moved to another location, use the following code:

<? 
header('Status: 301 Moved Permanently', false, 301); 
header('Location: new_address'); 
?>

For example, this page has the following code:

<? 
header('Status: 301 Moved Permanently', false, 301); 
header('Location: /pc/imprimante.php3'); 
exit(); 
?>

When you click on the link above, you are automatically redirected to this page. Moreover, it is a permanent redirection (Status: 301 Moved Permanently). So, if you type the first URL into Google, you will automatically be redirected to the second, redirected link.

Interpretation of PHP Code

The PHP code located after the header() will be interpreted by the server, even if the visitor moves to the address specified in the redirection. In most cases, this means that you need a method to follow the header() function of the exit() function in order to decrease the load of the server:

<? 
header('Status: 301 Moved Permanently', false, 301); 
header('Location: address'); 
exit(); 
?>
查看更多
柔情千种
4楼-- · 2018-12-30 22:47
<?php
$url = "targetpage"
Function redirect$url(){
   If (headers_sent()) == false{
      Echo '<script>window.location.href="' . $url . '";</script>';
}}
?>
查看更多
素衣白纱
5楼-- · 2018-12-30 22:49

Many of these answers are correct, but they assume you have an absolute URL, which may not be the case. If you want to use a relative URL and generate the rest, then you can do something like this...

$url = 'http://' . $_SERVER['HTTP_HOST'];            // Get the server
$url .= rtrim(dirname($_SERVER['PHP_SELF']), '/\\'); // Get the current directory
$url .= '/your-relative/path-goes/here/';            // <-- Your relative path
header('Location: ' . $url, true, 302);              // Use either 301 or 302
查看更多
怪性笑人.
6楼-- · 2018-12-30 22:49

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.

查看更多
十年一品温如言
7楼-- · 2018-12-30 22:50

1. Using header function with exit()

<?php 
     header('Location: target-page.php');
     exit();
?>

but if you use header function then some times you will get "warning like header already send" to resolve that do not echo or print before sending headers or you can simply use die() or exit() after header function.

2. Without header

<?php 
    echo "<script>location.href='target-page.php';</script>";
?>

here you will not face any problem

3. Using header function with ob_start() and ob_end_flush()

<?php
ob_start(); //this should be first line of your page
header('Location: target-page.php');
ob_end_flush(); //this should be last line of your page
?>
查看更多
登录 后发表回答