header (location) does not redirect

1

According to my program if I enter a user or password that is not registered, I should redirect myself back to the login page until a valid user enters, but do not grab it instead go to the check_login.php and it stays and does not return .. but it does not. form login.php

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Login</title>

    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css">

    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.2/html5shiv.js"></script>
      <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
  </head>
  <body>
    <h1>Ingresa tus datos</h1>
    <div class="container">
      <div class="row">
        <div class="col-lg-8">
          <form class="" action="comprueba_login.php" method="post">
            <div class="form-group">
              <label for="">Usuario</label>
              <input type="text" class="form-control" id="" placeholder="" name="login">
              <label for="">Contraseña</label>
              <input type="password" class="form-control" id="" placeholder="" name="password"><br>
              <button type="submit" name="button" class="btn btn-success">Ingresar</button>
              <button type="reset" name="button" class="btn btn-danger">Cancelar</button>

            </div>


          </form>
        </div>
      </div>

    </div>

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
  </body>
</html>

check_login.php

    <?php
try {

  $base = new PDO("mysql:host=localhost;dbname=pruebas;charset=utf8","root","root");

  $base->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
  $sql = "select * from usuarios_pass where usuario = :login and password = :password";
  $resultado = $base->prepare($sql);
  $login = htmlentities(addslashes($_POST['login']));
  $password = htmlentities(addslashes($_POST['password']));

  $resultado->bindValue(":login",$login);
  $resultado->bindValue(":password",$password);
  $resultado->execute();

  if ($resultado->rowCount() !=0){
    //echo "usuario registrado";
  }else{
    header ('location:login.php');
  }
} catch (Exception $e) {
  die ("Error al conectar" . $e->getMessage());
}


?>

Project directory:

    
asked by jeancarlos733 22.02.2017 в 05:00
source

2 answers

1

The format to redirect is

header('Location: http://www.example.com/');

Which in your case would be

header('Location: login.php');

That subtle difference may generate the error. But it may also be that, given your folder structure, the internal location of login.php , which is relative to comprueba_login.php , is different from the url with which you serve it.

You could try

header('Location: http://localhost/1login_pdo/login.php');

Or the absolute url that corresponds depending on where you put the root of your project.

    
answered by 22.02.2017 / 12:04
source
1

try with:

<?php "<script> window.location.replace('login.php') </script>" ?>
    
answered by 22.02.2017 в 05:54