是否可以通过使用PHP将用户重定向到不同的页面?
假设用户转到www.example.com/page.php,我想将其重定向到www.example.com/index.php,那么在不使用元刷新的情况下,我如何做到这一点?有可能吗?
这甚至可以保护我的页面免受未授权用户的攻击。
是否可以通过使用PHP将用户重定向到不同的页面?
假设用户转到www.example.com/page.php,我想将其重定向到www.example.com/index.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: https://www.example.com/redirect.php");
直接重定向到此链接https://www.example.com/redirect.php
$redirect = "https://www.example.com/redirect.php";
header("Location: $redirect");
首先获取$redirect值,然后重定向到[value],如:https://www.example.com/redirect.php
这些答案中的大多数都忘记了一个非常重要的步骤!
header("Location: myOtherPage.php");
die();
离开这条至关重要的第二条线可能会让你最终登上《每日WTF》。问题是,浏览器不必尊重页面返回的标题,因此忽略了标题,页面的其余部分将在没有重定向的情况下执行。
header('位置:http://www.yoursite.com/new_page.html' );
有多种方法可以实现您的目标。
一种常见的方法是设置收割台位置:
header("Location : www.example.com/index.php");
exit();
如果您在Apache上运行,也可以使用.htaccess进行重定向。
Redirect 301 / http://new-site.com/