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

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

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


当前回答

Use:

<?php
    header('Location: redirectpage.php');
    header('Location: redirectpage.php');
    exit();
    echo "<script>location.href='redirectpage.php';</script>";
?>

这是一个普通的PHP重定向,但您可以通过以下代码在几秒钟内创建一个重定向页面:

<?php
    header('refresh:5;url=redirectpage.php '); // Note: here 5 means 5 seconds wait for redirect.
?>

其他回答

您可以使用以下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
    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头函数进行重定向。您需要设置输出缓冲区,以便浏览器不会向屏幕发出重定向警告。

ob_start();
header("Location: " . $website);
ob_end_flush();
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: http://www.mywebsite.com/otherpage.php" );

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

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