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

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

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


当前回答

使用标头函数进行路由

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

假设我们想要从A.php文件路由到B.php,那么我们需要借助<button>或<A>。让我们看一个例子

<?php
if(isset($_GET['go_to_page_b'])) {
    header('Location: B.php');
    exit();

}
?>

<p>I am page A</p>
<button name='go_to_page_b'>Page B</button>

B.php语言

<p> I am Page B</p>

其他回答

我喜欢数秒后的重定向

<?php

header("Refresh: 3;url=https://theweek.com.br/índex.php");

是的,您可以使用header()函数,

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

最好的做法是在header()函数之后立即调用exit()函数,以避免执行以下代码。

根据文档,在发送任何实际输出之前,必须调用header()。

有多种方法可以做到这一点,但如果您更喜欢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

在语义网络的前夜,正确性是需要考虑的问题。不幸的是,PHP的“Location”标头仍然使用HTTP302重定向代码,严格来说,这不是最佳的重定向代码。它应该使用的是303。

W3C很高兴地提到,303标头与“许多HTTP/1.1之前的用户代理”不兼容,这相当于当前使用的浏览器。因此,302是一件遗物,不应该使用。

…或者你可以像其他人一样忽略它。。。

我已经回答了这个问题,但我会再回答一次,因为在此期间我了解到,如果您在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)