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

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

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


当前回答

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.
?>

其他回答

您可以尝试使用

header('Location:'.$your_url)

有关更多信息,请参阅php官方文档

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()!

有多种方法可以实现您的目标。

一种常见的方法是设置收割台位置:

header("Location : www.example.com/index.php");

exit();

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

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

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

是的,可以使用PHP。我们将重定向到另一个页面。

尝试以下代码:

<?php
    header("Location:./"); // Redirect to index file
    header("Location:index.php"); // Redirect to index file
    header("Location:example.php");
?>