header (location) does not redirect


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">
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <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>
    <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>




    <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>


try {

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

  $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']));


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


Project directory:

asked by jeancarlos733 22.02.2017 в 05:00

2 answers


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

try with:

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