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

假设用户转到www.example.com/page.php,我想将其重定向到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中重定向,请使用:

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

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

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

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

当用户启动时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>”;}?>

1.使用内置PHP函数header

a) 无参数的简单重定向

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

b) 使用GET参数重定向

<?php
      $id = 2;
      header("Location: index.php?id=$id&msg=succesfully redirect");
  ?>

2.在PHP中使用JavaScript重定向

a) 无参数的简单重定向

<?php
     echo "<script>location.href='index.php';</script>";
 ?>

b) 使用GET参数重定向

<?php
     $id = 2;
     echo "<script>location.href='index.php?id=$id&msg=succesfully redirect';</script>";
   ?>

有多种方法可以实现您的目标。

一种常见的方法是设置收割台位置:

header("Location : www.example.com/index.php");

exit();