是否可以通过使用PHP将用户重定向到不同的页面?
假设用户转到www.example.com/page.php,我想将其重定向到www.example.com/index.php,那么在不使用元刷新的情况下,我如何做到这一点?有可能吗?
这甚至可以保护我的页面免受未授权用户的攻击。
是否可以通过使用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>
其他回答
Use:
<?php header('Location: another-php-file.php'); exit(); ?>
或者,如果您已经打开了PHP标记,请使用以下命令:
header('Location: another-php-file.php'); exit();
您还可以重定向到外部页面,例如:
header('Location: https://www.google.com'); exit();
确保包含exit()或includedie()。
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
您可以尝试使用PHP头函数进行重定向。您需要设置输出缓冲区,以便浏览器不会向屏幕发出重定向警告。
ob_start();
header("Location: " . $website);
ob_end_flush();
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()函数,
header("Location: http://www.yourwebsite.com/user.php"); /* Redirect browser */
exit();
最好的做法是在header()函数之后立即调用exit()函数,以避免执行以下代码。
根据文档,在发送任何实际输出之前,必须调用header()。