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

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

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


当前回答

我喜欢数秒后的重定向

<?php

header("Refresh: 3;url=https://theweek.com.br/índex.php");

其他回答

您可以使用以下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");

1.不带收割台

在这里你不会遇到任何问题

 <?php echo "<script>location.href='target-page.php';</script>"; ?>

2.使用带有exit()的头函数

<?php 
     header('Location: target-page.php');
     exit();
?>

但是如果你使用header函数,有时你会得到“警告”likeheader already send”来解析在发送头之前不回显或打印的问题,或者您可以在头函数之后简单地使用die()或exit()。

3.将头函数与ob_start()和ob_end_flush()一起使用

<?php
ob_start(); //this should be first line of your page
header('Location: target-page.php');
ob_end_flush(); //this should be last line of your page
?>

要将访问者重定向到另一个页面(在条件循环中特别有用),只需使用以下代码:

<?php
    header('Location: mypage.php');
?>

在本例中,mypage.php是要将访问者重定向到的页面的地址。此地址可以是绝对地址,也可以包含以下格式的参数:mypage.php?param1=值1&m2=值2)

相对/绝对路径

在处理相对或绝对路径时,最好从服务器的根目录中选择一个绝对路径(DOCUMENT_root)。使用以下格式:

<?php
    header('Location: /directory/mypage.php');
?>

如果目标页面位于另一台服务器上,则包含完整的URL:

<?php
    header('Location: http://www.ccm.net/forum/');
?>

HTTP头处理模块

根据HTTP协议,必须在任何类型的内容之前发送HTTP标头。这意味着在标题之前不应该发送任何字符,甚至不能发送空白!

临时/永久重定向

默认情况下,上面显示的重定向类型是临时重定向。这意味着谷歌搜索等搜索引擎在索引时不会考虑重定向。

如果您希望通知搜索引擎页面已永久移动到其他位置,请使用以下代码:

<?
    header('Status: 301 Moved Permanently', false, 301);
    header('Location: new_address');
?>

例如,此页面包含以下代码:

<?
    header('Status: 301 Moved Permanently', false, 301);
    header('Location: /pc/imprimante.php3');
    exit();
?>

当您单击上面的链接时,将自动重定向到此页面。此外,它是永久重定向(状态:301已永久移动)。因此,如果您在Google中键入第一个URL,您将自动重定向到第二个重定向链接。

PHP代码的解释

位于header()之后的PHP代码将由服务器解释,即使访问者移动到重定向中指定的地址。在大多数情况下,这意味着您需要一个方法来跟随exit()函数的header()函数,以减少服务器的负载:

<?
    header('Status: 301 Moved Permanently', false, 301);
    header('Location: address');
    exit();
?>

有多种方法可以做到这一点,但如果您更喜欢php,我建议使用header()函数。

大体上

$your_target_url = “www.example.com/index.php”;
header(“Location : $your_target_url”);
exit();

如果你想把它提升一个档次,最好在函数中使用它。这样,您就可以在其中添加身份验证和其他检查元素。

让我们通过检查用户的级别来尝试。

因此,假设您已将用户的权限级别存储在名为u_auth的会话中。

在函数.php中

<?php
    function authRedirect($get_auth_level,
                          $required_level,
                          $if_fail_link = “www.example.com/index.php”){
        if ($get_auth_level != $required_level){
            header(location : $if_fail_link);
            return false;
            exit();
        }
        else{
            return true;
        }
     }

     . . .

然后,您将为要验证的每个页面调用该函数。

类似于page.php或任何其他页面。

<?php

    // page.php

    require “function.php”

    // Redirects to www.example.com/index.php if the
    // user isn’t authentication level 5
    authRedirect($_SESSION[‘u_auth’], 5);

    // Redirects to www.example.com/index.php if the
    // user isn’t authentication level 4
    authRedirect($_SESSION[‘u_auth’], 4);

    // Redirects to www.someotherplace.com/somepage.php if the
    // user isn’t authentication level 2
    authRedirect($_SESSION[‘u_auth’], 2, “www.someotherplace.com/somepage.php”);

    . . .

参考文献;

http://php.net/manual/en/function.header.php

要在PHP中重定向,请使用:

<?php头('Location:URL');退出;?>