because it does not redirect the php form remotely

0

I am a total novice in programming and on this website. I am working a page with php with password for access to another page. within the site, when doing the tests In Localhost it works well, but when I work with my server it does not redirect me to any page the complete code is:

<!doctype html>
<html>
<head>
 <meta charset="utf-8">
 <title>Linkar 1</title>
 <style>
  * {
   margin: 0;
   padding: 0;
  }

  #caja0 {
   font-family: sans-serif;
   background: #E0E0E0;
   width: 95%;
   border-radius: 10px;
   border: 2px solid red;
   padding: 1%;
   padding-bottom: 2%;
   margin: 1% auto;
  }

  h3 {font-size: 1.5em;
   text-align: center;
  }

  hr {
   border: solid 2px red;
   margin: 1em;
  }

  .rojo {
color: #FF0004;
 font-weight: bold;
  }
  #verde{color: #09FF00;}
  h1{
        text-align:center;
    }

    table{
        background-color:#FFC;
        padding:25px;
        border:#666 5px solid;
    }

    .no_validado{
        font-size:18px;
        color:#F00;
        font-weight:bold;
    }

    .validado{
        font-size:18px;
        color:#0C3;
        font-weight:bold;
    }

</style>
</head>

<body>

 <div id="caja0">

  <h3>Linkar a otros sitios con contraseña</h3>

<form method="post" action="<?php echo $_SERVER["PHP_SELF"];?>">
 <table width="48%" align="center">
  <tr>
   <td>Pon contraseña:</td>
   <td><label for="nombre_usuario"></label>
    <input type="text" name="nombre_usuario" id="nombre_usuario">
   </td>
  </tr>
  <tr>

  </tr>
  <tr>
   <td>&nbsp;</td>
   <td>&nbsp;</td>
  </tr>
  <tr>
   <td colspan="2" align="center"><input type="submit" name="enviando" id="enviando" value="Enviar">
   </td>
  </tr>
 </table>
</form>
  <?php
  /*Este código comprueba el envío del formulario*/
  if(isset($_POST["enviando"])){
   /*$_POST es una var Super Global y son Arrays y eso es lo que estamos haciendo con este formulario*/
   /*Con estas var almacenamos lo que el usuario ha introducido en los cuadros de texto en caso de que pulse el botón de Enviar  */
   $usuario=$_POST["nombre_usuario"];


   if($usuario=="Juan"){

   //echo(" Puedes entrar ya");
   /*Codigo para redirrecionar a otra pág dentro el sitio*/
   header('Location: /Probando.html');
   /*Codigo para redirrecionar a otra WEB*/
    //header('Location:http://jpbenavente.com/index.html');

   }else{ echo("No puedes entrar nunca");


        }//Del 2º if

   };//DEL iF 
  ?>

 </div>


</body>
</html>

Thanks for your help

    
asked by Jesús 10.08.2018 в 21:20
source

1 answer

0

Probably the problem is that you are calling header but before that you are already returning the response to the request.

That is, this would be incorrect

<html>
<?php
header();
?>

While this would work, since you do the redirect before responding to the request.

<?php
header();
?>
<html>

Try changing your page, moving the part of php that the user checks at the beginning. Also, it is good practice to put a die or exit after the header. There are some scrappers that could ignore the redirect and follow the request (with potentially undesirable effects)

The code would look something like this:

<?php
/*Este código comprueba el envío del formulario*/
if(isset($_POST["enviando"])){
/*$_POST es una var Super Global y son Arrays y eso es lo que estamos haciendo con este formulario*/
/*Con estas var almacenamos lo que el usuario ha introducido en los cuadros de texto en caso de que pulse el botón de Enviar  */
$usuario=$_POST["nombre_usuario"];


if($usuario=="Juan"){

//echo(" Puedes entrar ya");
/*Codigo para redirrecionar a otra pág dentro el sitio*/
header('Location: /Probando.html');
die("You shall not pass!!"); // Es trabajo del navegador hacer el redirect. Abortamos si no lo hace
/*Codigo para redirrecionar a otra WEB*/
//header('Location:http://jpbenavente.com/index.html');

}else{ echo("No puedes entrar nunca");


    }//Del 2º if

};//DEL iF 
?>
<!doctype html>
<html>
<head>
 <meta charset="utf-8">
 <title>Linkar 1</title>
 <style>
  * {
   margin: 0;
   padding: 0;
  }

  #caja0 {
   font-family: sans-serif;
   background: #E0E0E0;
   width: 95%;
   border-radius: 10px;
   border: 2px solid red;
   padding: 1%;
   padding-bottom: 2%;
   margin: 1% auto;
  }

  h3 {font-size: 1.5em;
   text-align: center;
  }

  hr {
   border: solid 2px red;
   margin: 1em;
  }

  .rojo {
color: #FF0004;
 font-weight: bold;
  }
  #verde{color: #09FF00;}
  h1{
        text-align:center;
    }

    table{
        background-color:#FFC;
        padding:25px;
        border:#666 5px solid;
    }

    .no_validado{
        font-size:18px;
        color:#F00;
        font-weight:bold;
    }

    .validado{
        font-size:18px;
        color:#0C3;
        font-weight:bold;
    }

</style>
</head>

<body>

 <div id="caja0">

  <h3>Linkar a otros sitios con contraseña</h3>

<form method="post" action="<?php echo $_SERVER["PHP_SELF"];?>">
 <table width="48%" align="center">
  <tr>
   <td>Pon contraseña:</td>
   <td><label for="nombre_usuario"></label>
    <input type="text" name="nombre_usuario" id="nombre_usuario">
   </td>
  </tr>
  <tr>

  </tr>
  <tr>
   <td>&nbsp;</td>
   <td>&nbsp;</td>
  </tr>
  <tr>
   <td colspan="2" align="center"><input type="submit" name="enviando" id="enviando" value="Enviar">
   </td>
  </tr>
 </table>
</form>

 </div>


</body>
</html>
    
answered by 10.08.2018 / 21:39
source