【问题标题】:How redirect without header in php?如何在 php 中没有标头的重定向?
【发布时间】:2015-07-13 15:57:34
【问题描述】:

我有controllerLoginUsu.php

<?php

require "dao/daoLoginUsu.php";  

class LoginUsuario{

    public function setDatos($aInput) {

        $obj = json_decode($aInput, true);

       $Dao = new daoLoginUsuario();
       $Dao->setDataDato($obj);

       $msj = $Dao->setDataDato($obj);


      session_start();
      if ($msj === 'si') {
          $_SESSION['msj'] = "si";
          return $msj;
          header("http://localhost:8080/formulario_web/formulario/formulario_lazos.php");
          exit; 

      }
   } 
}
?>

在我开始会话并返回 $msj 后,我需要重定向,但使用标头不起作用。这种情况的其他解决方案?

对不起我的英语。

【问题讨论】:

标签: php redirect header


【解决方案1】:

你忘记了位置。

尝试:

header("Location: http://localhost:8080/formulario_web/formulario/formulario_lazos.php");

【讨论】:

    【解决方案2】:

    你永远不会到达 header() 调用:

     return $msj;  // terminate function IMMEDIATELY
     header("http://localhost:8080/formulario_web/formulario/formulario_lazos.php"); // never reached
    

    return 应该在之后 header()

    【讨论】:

      【解决方案3】:

      如果您想在没有标头的情况下进行重定向,请尝试以下操作:

      echo "<script>";
      echo "location.replace('classes.php?add=sucess')";
      echo "</script>";
      

      【讨论】:

      • 这实际上是一种非常讨厌的方式,与header() 做同样的事情,因为整个页面被构建->发送到浏览器->然后 DOM 由浏览器构建->AND然后一切都被抛弃了。 非常浪费
      【解决方案4】:

      答案其实是下面两种的混合

      如果您发出header(),您不想return 任何东西,因为您正在强制加载新表单。 return 当然会停止执行此方法,在您的情况下,标头甚至不会被执行。

      另外,您忘记在header() 语句中添加location: 部分。

      所以

      if ($msj === 'si') {
          $_SESSION['msj'] = "si";
          header("Location: http://localhost:8080/formulario_web/formulario/formulario_lazos.php");
          exit; 
        }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-11-18
        • 2021-07-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-04-05
        • 2011-09-22
        相关资源
        最近更新 更多