doubts about php authentication

Asked

Viewed 56 times

1

When I take the test of login, I’m going to autenticar.php, but the screen is blank instead of directing to the main page.

msql and php command tested and working)

<script language="javascript">
    function sucesso(){
        setTimeout("window.location='principal.html'", 4000);
    }
    function failed(){
        setTimeout("window.location='login.html'", 4000);
    }
</script>

    $consulta = mysqli_query("SELECT * FROM usuarios WHERE usuario = '$email' AND senha = '$pass'") or die (mysqli_error($conexao));
    $linhas = mysqli_num_rows($consulta);

    if($linhas == 0){
        echo"O login falhou. Você será redirecionado para a página de login em 4 segundos.";
        echo"<script language='javascript'>failed()</script>";
    } else {
        $_SESSION["email"]=$_POST["email"];
        $_SESSION["senha"]=$_POST["pass"];
        echo"Você foi logado com sucesso. Redirecionando em 4 segundos.";
        echo"<script language='javascript'>sucesso()</script";
    }
?>

  • 2

    I believe you have the ability to craft a better title than that (How to choose a good title?) Press Ctrl+U on the blank page, which appears?

  • If any answer solved your problem mark it as accepted. See how in https://i.stack.Imgur.com/evLUR.png and see why in https://pt.meta.stackoverflow.com/questions/1078/como-e-por-que-aceitar-uma-resposta/1079#1079

2 answers

1


Hit your statement with the connection string

example

$con = new mysqli ("localhost", "USUARIO", "SENHA", "NOME_DB");

$query = mysqli_query($con,"SELECT......

$consulta = mysqli_query($con,"SELECT * FROM usuarios WHERE usuario = '$email' AND senha = '$pass'") or die (mysqli_error($conexao));

Another detail the script closing tag </script missing a >

echo"<script language='javascript'>sucesso()</script";

correct for

echo"<script language='javascript'>sucesso()</script>";

0

At the end of the php script, if Login is ok, add the line:

header('location: login_ok.php');

Where "login_ok.php" would be the name of the page to be redirected.

  • In addition to what Rodrigo said, you won’t need to redirect via Javascript like you did.

  • What is the relationship between redirecting with JS or with Location and the problem presented in the question?

  • No need to redirect with JS. Once inside PHP (server), it itself can redirect the user instead of returning it to the JS redirect browser.

  • Yes, but I don’t understand why it solves the problem - to tell you the truth, I don’t even understand what the problem is.

  • That solves it. Eduardo said that after login the screen turns white and goes nowhere. Putting the command I mentioned in php, when logging in the user can be redirected "into" the system.

Browser other questions tagged

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