The code does not respect ifs

-1

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

<?
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);
?>
    
asked by anonymous 25.04.2015 / 06:40

1 answer

1

You are actually updating the page when you use META REFRESH. So when the script stops at the second IF, it reloads the page d displays the alert. However, the page is executed again, but this time there is no longer $ _POST, so the script falls on the first IF because there was no user. And it will stay that way.

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

Or by redirecting through the 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>
    
25.04.2015 / 07:04