Header Location is not directing page ( PHP )

Asked

Viewed 858 times

0

<?php
    // conexão
    require_once'connect.php';

    // Sessão
    session_start();


    if(isset($_POST["usuario"])){

        $erros = array();
        $usuario = mysqli_escape_string($connect,$_POST["usuario"]);
        $senha = mysqli_escape_string($connect,$_POST["senha"]);

        if(empty($usuario) or empty($senha)){
            $erros[] = "<li> O campo Usuário/Senha precisa ser preenchido ! </li>";
        }else{
            $sql = "SELECT login FROM usuarios WHERE login = '$usuario'";
            $resultado = mysqli_query($connect, $sql);

            if(mysqli_num_rows($resultado) > 0){
                $senha = md5($senha);
                $sql = "SELECT * FROM usuarios WHERE login = '$usuario' AND senha = '$senha'";
                $resultado = mysqli_query($connect, $sql);

                if(mysqli_num_rows($resultado) == 1){
                    $dados = mysqli_fetch_array($resultado);
                    mysqli_close($connect);

                    $_SESSION['logado'] = TRUE;
                    $_SESSION['id_usuario'] = $dados['id'];

                    **header('Location: home.php');**
                }else{
                   $erros[] = "<li> Usuário ou senha não conferem.</li>"; 
                }

            }else{
                $erros[] = "<li> Usuário não cadastrado.</li>";
            }
        }
    }
?>

Login System

    </header>
    <main>  

         <form action=<?php echo $_SERVER['PHP_SELF']; ?> method="POST">
            <h2> Login </h2>
            <input type="text" name="usuario" placeholder="Usuário">
            <input type="password" name="senha" placeholder="Senha">
            <input type="submit" value="Login">

            <?php
                if( !empty($erros) ) {
            ?>

            <p> <?php foreach($erros as $erro){
                        echo $erro;
                } ?></p>

            <?php
                }
            ?>

        </form> 
    </main>
    <footer>

    </footer>
</body>

**Everything is working, but it does not redirect to the home page.php, I do not know what can be. I appreciate the help. **

  • home.php files and the file that has the access conditions are in the same folder hierarchy? I made an example of the test.php file accessing home.php being that home.php is in a folder and the test.php file is outside the folder. 'header( 'Location: .. /testefolder/home.php' );'

  • except for those asterisks I don’t see why it doesn’t work.

  • Nothing related to the error, which in my view does not exist in the code. Only one way to save processing. The way the code is, obligatorily, if the user exists, will perform 2 SELECTS. Well, if the user exists, we will soon at last. Now if login and password do not return, do the other SELECT with login to check if it exists, if it exists inform that password does not check and otherwise inform that User not registered. See suggestion at https://anotepad.com/notes/njdb3w

1 answer

-2

I imagine the error may be because you are referring directly to the php file, and not to a valid URL.

try to change:

header('Location: home.php');

To:

header('Location: http://localhost/projeto/home.php');

Browser other questions tagged

You are not signed in. Login or sign up in order to post.