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

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

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


当前回答

我们可以通过两种方式实现:

当用户启动时https://bskud.com/PINCODE/BIHAR/index.php然后重定向到https://bskud.com/PINCODE/BIHAR.php通过以下PHP代码<?php文件header(“位置:https://bskud.com/PINCODE/BIHAR.php");出口?>将上述代码保存在https://bskud.com/PINCODE/BIHAR/index.php如果任何条件为真,则重定向到另一页:<?php文件$myVar=“bskud”;如果($myVar==“bskud”){?><script>window.location.href=“https://bskud.com“;</script><?php文件}其他{echo“<b>再次检查网站名称</b>”;}?>

其他回答

使用header()函数发送HTTP位置标头:

header('Location: '.$newURL);

与一些人的想法相反,die()与重定向无关。仅当您希望重定向而不是正常执行时使用它。

文件example.php:

<?php
    header('Location: static.html');
    $fh = fopen('/tmp/track.txt', 'a');
    fwrite($fh, $_SERVER['REMOTE_ADDR'] . ' ' . date('c') . "\n");
    fclose($fh);
?>

三次执行的结果:

bart@hal9k:~> cat /tmp/track.txt
127.0.0.1 2009-04-21T09:50:02+02:00
127.0.0.1 2009-04-21T09:50:05+02:00
127.0.0.1 2009-04-21T09:50:08+02:00

恢复-强制性die()/exit()是一些与实际PHP无关的都市传说。这与客户端“尊重”Location:标头无关。无论使用何种客户端,发送头都不会停止PHP执行。

header('位置:http://www.yoursite.com/new_page.html' );

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

Use:

<?php
    $url = "targetpage"
    function redirect$url(){
        if (headers_sent()) == false{
            echo '<script>window.location.href="' . $url . '";</script>';
        }
    }
?>

您可以尝试使用

header('Location:'.$your_url)

有关更多信息,请参阅php官方文档