The code does not respect ifs

Asked

Viewed 67 times

-1

Hello, in this code when it runs it does not respect ifs, it goes straight through running everything. For example, if the message "This user does not exist" appears it should simply end. but also the message "Incorrect password".

<?
include "conection.php";

$login = $_POST['login'];
$senha = $_POST['senha'];

$sql = mysqli_query($con, "SELECT * FROM usuarios WHERE login = '$login'"); 


while($linha = mysqli_fetch_array($sql))
{
    $senha_db = $linha['senha'];
}

$cont = mysqli_num_rows($sql);

if($cont == 0)
{
    echo "<meta http-equiv='refresh' content='0; url=index.php'>
    <script type='text/javascript'>alert('Este usuario não existe')</script>";      
}
else
{
    if($senha_db != $senha)
    {
        echo "<meta http-equiv='refresh' content='0; url=index.php'>
        <script type='text/javascript'>alert('Senha incorreta')</script>";  
    }
    else
    {
        session_start();

        $_SESSION['login_usuario'] = $login;    
        $_SESSION['senha_usuario'] = $senha;

        header("Location: perfil.php"); 
    }
}

mysqli_close($con);
?>

1 answer

1

In fact you are updating the page when you use META REFRESH. Then when the script stops in the second IF, it reloads the page d displays the alert. However, the page runs again, but this time there is no more $_POST, so the script falls into the first IF because there was no user. And it’ll stay that way.

I suggest you stop using META REFRESH and start using php itself:

Or redirecting by header:

header("location: sua_pagina.php");

Or using ifs to display the Javascript alert:

<script>
var _msg = '<?php echo condição ? "Mensagem verdadeira" : "mensagem falsa"; ?>';
alerta(_msg);
</script>

Browser other questions tagged

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