简体   繁体   中英

How redirect without header in php?

I have 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; 

      }
   } 
}
?>

After I start session and return $msj I need redirect but with header don't work. some other solution to this case?

Sorry my english.

If you want to redirect without header, try this:

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

You forgot the Location.

try:

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

You never ever reach the header() call:

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

return should come AFTER header() .

The answer is actually a mixture of 2 of the below

If you are issuing a header() you dont want to return anything as you are forcing the loading of a new form. The return will of course stop execution of this method and in your case the header will then not even get executed.

Also you forgot to add the location: part of the header() statement.

So

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM