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

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

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


当前回答

要在PHP中重定向,请使用:

<?php头('Location:URL');退出;?>

其他回答

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

您可以使用以下JavaScript方法

self.location=“http://www.example.com/index.php";window.location.href=“http://www.example.com/index.php";文档位置href='http://www.example.com/index.php';window.location.replace(“http://www.example.com/index.php");

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

尝试以下代码:

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

要在PHP中重定向,请使用:

<?php头('Location:URL');退出;?>

试试这个

 $url = $_SERVER['HTTP_REFERER'];
 redirect($url);