Problem with Php and mysql - Simple login

1

Good evening!

I'm making a simple website, which displays a box with username and password that will be sent by form via the post method to the valida.php page . When comparing the user and password entered by the user, with the user and password predefined in an if / else, the page redirects to main.html, which is the correct one. However, when I change the code to get directly into the database, when the user enters the credentials and presses the button, it is sent to the same page .. I can not understand what is wrong ...

Follow the codes:

index.php

   <form class="login100-form validate-form" method="POST" action="valida.php">
                           
<div class="wrap-input100 validate-input">
    <input class="input100" type="password" id="inputSenha" name="senha" placeholder="Digite a Senha" required>
    <span class="focus-input100" data-placeholder="&#xf191;"></span>
</div>

<div id="botao">
    <input type="submit" value="ENTRAR" name="botao" id="entrar" class="btn efeito efeito-1 botaoEnviar" style="color: black; font-weight: bold" autofocus>
</div>

valida.php

<?php

    include_once("conexao.php");
    if((isset($_POST['user'])) && isset($_POST['senha']))
    {

        $usuario = mysqli_real_escape_string($conn, $_POST['user']);
        $senha = mysqli_real_escape_string($conn, $_POST['senha']);
        $senha = md5($senha);

        $sql = "SELECT * FROM tb_login WHERE user = '$usuario' && senha = '$senha' LIMIT 1";
        $result = mysqli_query($conn, $sql);
        $resultado = mysqli_fetch_assoc($result);

        if(empty($resultado))
        {
            $_SESSION['loginErro'] = "Usuario ou senha incorretos.";
            header("Location: index.php");
        }
        elseif(isset($resultado))
        {
            header("Location: main.html");
        }
        else
        {
            $_SESSION['loginErro'] = "Usuario ou senha incorretos.";
        }
    }
    else
    {
        $_SESSION['loginErro'] = "Usuario ou senha incorretos.";
        header("Location: index.php");
    }

?>

connection.php

<?php
    $servidor = "localhost";
    $usuario  = "root";
    $senha = "";
    $dbname = "bd_otica";

    $conn = mysqli_connect($servidor, $usuario, $senha, $dbname);

    if(!$conn)
    {
        die("Falha na conexão: " . mysqli_connect_error());
    }else
    {}

?>

Thanks for any help!

    
asked by anonymous 20.08.2018 / 03:04

2 answers

1
  

As commented by @Bacco, change && by AND in SQL.

The empty($resultado) will always be false because $resultado = mysqli_fetch_assoc($result); will return an array, even if the result of the user query and password returns an empty array.

Just make a if and else if the command mysqli_fetch_assoc($result) is true, that is, found something in the database that matches the username and password:

if(mysqli_fetch_assoc($result)){
   header("Location: main.html");
}else{
   $_SESSION['loginErro'] = "Usuario ou senha incorretos.";
   header("Location: index.php");
}

Complete code:

<?php
    include_once("conexao.php");
    if((isset($_POST['user'])) && isset($_POST['senha']))
    {

        $usuario = mysqli_real_escape_string($conn, $_POST['user']);
        $senha = mysqli_real_escape_string($conn, $_POST['senha']);
        $senha = md5($senha);

        $sql = "SELECT * FROM tb_login WHERE user = '$usuario' AND senha = '$senha'";
        $result = mysqli_query($conn, $sql);

        if(mysqli_fetch_assoc($result)){
            header("Location: main.html");
        }else{
            $_SESSION['loginErro'] = "Usuario ou senha incorretos.";
            header("Location: index.php");
        }
    }
?>
    
20.08.2018 / 03:47
0

Your problem in this system friend @Cayo Eduardo Silveira, is due only to the fact that the code of the form that you posted in the question, it does not have the field "user" that is checked in the first condition if((isset($_POST['user'])) && isset($_POST['senha'])) , hence the code directs to the last else , so it does not satisfy condition and consequently it redirects to $_SESSION['loginErro'] = "Usuario ou senha incorretos.";header("Location: index.php");

Check there and let me know

    
20.08.2018 / 14:23