Header: location - Does not work

3

Perform a project that required the pages to have .php extension

My problem is that when the page must redirect with the label: header("nuevapgina.php"); , it simply remains blank, and in the project I use it in 3 different occasions.

  

index.php - > login.php

     

login.php - > do_login.php (users are searched in the DB)

     

anyPagina.php - > dologout.php (exits the system)

For the first case: index.php - > login.php I had to add the basic html page structure below the php part so that it would work.

<html>

<head></head>

<body>

</body>

</html>

However ... I do not understand why it requires this, now if I try to do the same in the other cases the page is blank and static.

What can I do?

Code: do_login.php

<?php
include 'conexion.php';

ob_start();

$usuario = $_POST['username'];
$pass = $_POST['password'];

if(empty($usuario) || empty($pass)){
header("Status: 301 Moved Permanently");
header("Location:www.mercadeopernod.tk/index.php");
exit();
}

$pdo = Database::connect(); 
$sql = "SELECT * FROM usuarios WHERE nom_usuario='" . $usuario . "'";
$stmt = $pdo->query($sql);
if($stmt->rowCount() != 0){

        foreach ($stmt as $row) {

                if($row['ide_usuario'] == $pass){

                session_start();
                $_SESSION['usuario'] = $usuario;

                     if($row['rol_usuario'] == 'ADMINISTRADOR'){
                        Database::disconnect();      
                        header("Status: 301 Moved Permanently");        
                        header("Location:www.mercadeopernod.tk/index-administrador.php");
                        exit();
                     }else{
                        Database::disconnect();
                        header("Status: 301 Moved Permanently");
                        header("Location:www.mercadeopernod.tk/index-mercadeo.php");
                        exit();
                     }

                }else{
                Database::disconnect(); 
                header("Status: 301 Moved Permanently");
                header("Location:http:www.mercadeopernod.tk/index.php");
                exit();
                }
        }

}else{
    Database::disconnect();
    header("Status: 301 Moved Permanently");
    header("Location:http:www.mercadeopernod.tk/index.php");
    exit();
}


?>

I have tried in the following ways:

header("Status: 301 Moved Permanently");
header("Location:www.mercadeopernod.tk/index.php");
exit();



header("Status: 301 Moved Permanently");
header("Location:http://www.mercadeopernod.tk/index.php");
exit();



header("Location:www.mercadeopernod.tk/index.php");




header("Location:www.mercadeopernod.tk/index.php");
exit();



header("Location:index.php");
exit();



header("Location: index.php");


header("Location:http://www.mercadeopernod.tk/index.php");
exit();

In general I do not require HTML for this part, is it necessary? because the page only stays blank and does not generate any errors ..

What other way could I implement?

Thank you.

EDIT:

Edit, the types of attempts I made by adding http://

The address in the browser:

Has something to do with the browser telling me that it is not a secure connection?

Edition 2:

index.php (login)

<!DOCTYPE html>
<html lang="en">

  <head>

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="description" content="">
    <meta name="author" content="">

    <title>Login</title>

    <!-- Bootstrap core CSS -->
    <link href="vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">

    <!-- Custom fonts for this template -->
    <link href="vendor/font-awesome/css/font-awesome.min.css" rel="stylesheet" type="text/css">
    <link href="https://fonts.googleapis.com/css?family=Montserrat:400,700" rel="stylesheet" type="text/css">
    <link href="https://fonts.googleapis.com/css?family=Lato:400,700,400italic,700italic" rel="stylesheet" type="text/css">

    <!-- Plugin CSS -->
    <link href="vendor/magnific-popup/magnific-popup.css" rel="stylesheet" type="text/css">

    <!-- Custom styles for this template -->
    <link href="css/freelancer.min.css" rel="stylesheet">
    <link href="css/form.css" rel="stylesheet">

    <!-- FAVICON IMG -->
    <link rel="shortcut icon" href="img/favico.png">

  </head>

  <body id="page-top">

    <!-- Navigation -->
    <nav class="navbar navbar-expand-lg bg-secondary fixed-top text-uppercase" id="mainNav">
      <div class="container">
          <center>
             <a class="navbar-brand js-scroll-trigger" href="#page-top">Sistema Inventario</a>
          </center>         
      </div>

    </nav>

    <!-- Header -->
      <div class="container" style="padding-top: 50px"></div>


    <!--  Section -->
    <section id="ubicacion.php">
      <div class="container">

        <div class="container" style="padding-top: 50px"></div>

                 <form  class="form-basic"  action="do_login.php" method="post" id="fcrear">

                          <div class="form-title-row">
                              <h1>Login</h1>
                          </div>
                    <div class="container">
                      <div class="form-row">
                           <div class="controls">
                                <label class="control-label">Nombre</label>                            
                                <input placeholder="Nombre del Usuario" name="username" id="username">
                            </div>                          
                      </div>

                       <div class="form-row">
                           <div class="controls">
                                <label class="control-label">Contraseña</label>                            
                                <input placeholder="Password" name="password"  id="password" type="password">
                            </div>                          
                      </div>

                      <div class="red">&nbsp;<?php if(!empty($_SESSION["login_error"])){echo("USUARIO O CONTRASEÑA INCORRECTAS");} ?></div>

                          <div class="container" style="padding-top: 50px"></div>
                          <div class="form-actions">
                              <input type="submit"   value="Entrar" >
                              <!--<a style="width: 95px" class="btn btn-warning" onclick="this.form.submit()" >Entrar</a>-->
                          </div>
                    </div>
              </form>


      </div>
    </section>

    <!-- Footer -->
    <div class="copyright py-4 text-center text-white">
      <div class="container">
        <img class="img-fluid mb-5 d-block mx-auto" src="img/footer-logo.png">
        <small>Copyright &copy; 2018 -  Todos los derechos reservados</small>
      </div>
    </div>

    <!-- Scroll to Top Button (Only visible on small and extra-small screen sizes) -->
    <div class="scroll-to-top d-lg-none position-fixed ">
      <a class="js-scroll-trigger d-block text-center text-white rounded" href="#page-top">
        <i class="fa fa-chevron-up"></i>
      </a>
    </div>



    <!-- Bootstrap core JavaScript -->
    <script src="vendor/jquery/jquery.min.js"></script>
    <script src="vendor/bootstrap/js/bootstrap.bundle.min.js"></script>

    <!-- Plugin JavaScript -->
    <script src="vendor/jquery-easing/jquery.easing.min.js"></script>
    <script src="vendor/magnific-popup/jquery.magnific-popup.min.js"></script>

    <!-- Custom scripts for this template -->
    <script src="js/freelancer.min.js"></script>

  </body>

</html>
    
asked by Andres Felipe Diaz 05.03.2018 в 17:28
source

2 answers

-1

In the end the only way that I could make the page run according to my need is with javascript.

Very surely it is not a good practice (I would not know to say it for sure).

Code:

 header("Status: 301 Moved Permanently");
                header("Location:index.php");
                echo"<script language='javascript'>window.location='index.php'</script>;";
                exit();

I have added a javascript call later to the header: .

        echo"<script language='javascript'>window.location='index.php'</script>;";
    
answered by 05.03.2018 / 19:02
source
3

The address protocol is incorrect, you should indicate it correctly:

header("Location:http://www.mercadeopernod.tk/index.php");

Greetings

    
answered by 05.03.2018 в 17:39