是否可以通过使用PHP将用户重定向到不同的页面?

假设用户转到www.example.com/page.php,我想将其重定向到www.example.com/index.php,那么在不使用元刷新的情况下,我如何做到这一点?有可能吗?

这甚至可以保护我的页面免受未授权用户的攻击。


当前回答

现有答案汇总加上我自己的两分钱:

1.基本答案

您可以使用header()函数发送新的HTTP标头,但必须在任何HTML或文本之前(例如,在<!DOCTYPE…>声明之前)将其发送到浏览器。

header('Location: '.$newURL);

2.重要细节

die()或exit()

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

为什么应该使用die()或exit():每日WTF

绝对或相对URL

自2014年6月起,可以使用绝对URL和相对URL。请参阅RFC 7231,它取代了旧的RFC 2616,其中只允许绝对URL。

状态代码

PHP的“位置”标头仍然使用HTTP 302重定向代码,这是一个“临时”重定向,可能不是您应该使用的重定向。您应该考虑301(永久重定向)或303(其他)。

注意:W3C提到303标头与“许多HTTP/1.1之前的用户代理”不兼容。当前使用的浏览器都是HTTP/1.1用户代理。这对于许多其他用户代理(如蜘蛛和机器人)来说是不正确的。

3.文件

PHP中的HTTP Headers和header()函数

PHP手册的内容维基百科说什么W3C的说法

4.备选方案

您可以使用http_redirect($url);这需要安装PECL包PECL。

5.助手函数

此功能不包含303状态代码:

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

    exit();
}

Redirect('https://example.com/', false);

这更加灵活:

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

6.解决方法

如前所述,header()重定向只在写入任何内容之前生效。如果在midst HTML输出中调用,它们通常会失败。然后,您可以使用HTML标头解决方法(不是很专业!),如:

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

甚至是JavaScript重定向。

window.location.replace("https://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);

别忘了die()/ext()!

我已经回答了这个问题,但我会再回答一次,因为在此期间我了解到,如果您在CLI中运行(重定向无法发生,因此不应该exit()),或者您的Web服务器以(F)CGI的形式运行PHP(它需要预先设置的Status标头才能正确重定向),则会有一些特殊情况。

function Redirect($url, $code = 302)
{
    if (strncmp('cli', PHP_SAPI, 3) !== 0)
    {
        if (headers_sent() !== true)
        {
            if (strlen(session_id()) > 0) // If using sessions
            {
                session_regenerate_id(true); // Avoids session fixation attacks
                session_write_close(); // Avoids having sessions lock other requests
            }

            if (strncmp('cgi', PHP_SAPI, 3) === 0)
            {
                header(sprintf('Status: %03u', $code), true, $code);
            }

            header('Location: ' . $url, true, (preg_match('~^30[1237]$~', $code) > 0) ? $code : 302);
        }

        exit();
    }
}

我还处理了支持不同HTTP重定向代码(301、302、303和307)的问题,这在我之前的回答的评论中得到了解决。以下是描述:

301-永久移动302-找到303-见其他307-临时重定向(HTTP/1.1)

有多种方法可以做到这一点,但如果您更喜欢php,我建议使用header()函数。

大体上

$your_target_url = “www.example.com/index.php”;
header(“Location : $your_target_url”);
exit();

如果你想把它提升一个档次,最好在函数中使用它。这样,您就可以在其中添加身份验证和其他检查元素。

让我们通过检查用户的级别来尝试。

因此,假设您已将用户的权限级别存储在名为u_auth的会话中。

在函数.php中

<?php
    function authRedirect($get_auth_level,
                          $required_level,
                          $if_fail_link = “www.example.com/index.php”){
        if ($get_auth_level != $required_level){
            header(location : $if_fail_link);
            return false;
            exit();
        }
        else{
            return true;
        }
     }

     . . .

然后,您将为要验证的每个页面调用该函数。

类似于page.php或任何其他页面。

<?php

    // page.php

    require “function.php”

    // Redirects to www.example.com/index.php if the
    // user isn’t authentication level 5
    authRedirect($_SESSION[‘u_auth’], 5);

    // Redirects to www.example.com/index.php if the
    // user isn’t authentication level 4
    authRedirect($_SESSION[‘u_auth’], 4);

    // Redirects to www.someotherplace.com/somepage.php if the
    // user isn’t authentication level 2
    authRedirect($_SESSION[‘u_auth’], 2, “www.someotherplace.com/somepage.php”);

    . . .

参考文献;

http://php.net/manual/en/function.header.php

1.不带收割台

在这里你不会遇到任何问题

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

2.使用带有exit()的头函数

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

但是如果你使用header函数,有时你会得到“警告”likeheader already send”来解析在发送头之前不回显或打印的问题,或者您可以在头函数之后简单地使用die()或exit()。

3.将头函数与ob_start()和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
?>

Use:

<?php
    header('Location: redirectpage.php');
    header('Location: redirectpage.php');
    exit();
    echo "<script>location.href='redirectpage.php';</script>";
?>

这是一个普通的PHP重定向,但您可以通过以下代码在几秒钟内创建一个重定向页面:

<?php
    header('refresh:5;url=redirectpage.php '); // Note: here 5 means 5 seconds wait for redirect.
?>