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

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

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


当前回答

就像这里的其他人所说的,发送位置标头时:

header( "Location: http://www.mywebsite.com/otherpage.php" );

但在向浏览器发送任何其他输出之前,您需要执行此操作。

此外,如果您要使用此选项阻止未经身份验证的用户访问某些页面,如您所述,请记住,某些用户代理会忽略此选项,并继续在当前页面上运行,因此您需要在发送后死亡()。

其他回答

如果您在Apache上运行,也可以使用.htaccess进行重定向。

Redirect 301 / http://new-site.com/

您可以使用会话变量控制对页面的访问并授权有效用户:

<?php
    session_start();

    if (!isset( $_SESSION["valid_user"]))
    {
        header("location:../");
        exit();
    }

    // Page goes here
?>

http://php.net/manual/en/reserved.variables.session.php.

最近,我受到了网络攻击,并决定,我需要了解试图访问管理面板或web应用程序的保留部分的用户。

因此,我在文本文件中添加了IP地址和用户会话的日志访问,因为我不想打扰我的数据库。

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

尝试以下代码:

<?php
    header("Location:./"); // Redirect to index file
    header("Location:index.php"); // Redirect to index file
    header("Location:example.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>
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