是否可以通过使用PHP将用户重定向到不同的页面?
假设用户转到www.example.com/page.php,我想将其重定向到www.example.com/index.php,那么在不使用元刷新的情况下,我如何做到这一点?有可能吗?
这甚至可以保护我的页面免受未授权用户的攻击。
是否可以通过使用PHP将用户重定向到不同的页面?
假设用户转到www.example.com/page.php,我想将其重定向到www.example.com/index.php,那么在不使用元刷新的情况下,我如何做到这一点?有可能吗?
这甚至可以保护我的页面免受未授权用户的攻击。
当前回答
有多种方法可以实现您的目标。
一种常见的方法是设置收割台位置:
header("Location : www.example.com/index.php");
exit();
其他回答
header('位置:http://www.yoursite.com/new_page.html' );
使用标头函数进行路由
<?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>
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()!
1.使用内置PHP函数header
a) 无参数的简单重定向
<?php
header('Location: index.php');
?>
b) 使用GET参数重定向
<?php
$id = 2;
header("Location: index.php?id=$id&msg=succesfully redirect");
?>
2.在PHP中使用JavaScript重定向
a) 无参数的简单重定向
<?php
echo "<script>location.href='index.php';</script>";
?>
b) 使用GET参数重定向
<?php
$id = 2;
echo "<script>location.href='index.php?id=$id&msg=succesfully redirect';</script>";
?>
如果您在Apache上运行,也可以使用.htaccess进行重定向。
Redirect 301 / http://new-site.com/