Php Login Screen Not Authenticate

Asked

Viewed 61 times

0

I’m starting programming with PHP, Since My Login screen is not Receiving the User and password, this and the code of my Verification page, you can see what’s wrong please.

<?php
include('conexao.php');
if (empty($_POST['usuario']) || empty($_POST['senha'])){
    header('Location: index.php');
    exit();
}

$usuario = mysqli_real_escape_string($conexao, $_POST['usuario']);
$senha = mysqli_real_escape_string($conexao, $_POST['senha']);

$query = "SELECT * from cad.usuario where usuario = '{$usuario}' and senha = '{$senha}'";

$result = mysqli_query($conexao, $query);

$row = mysqli_num_rows($result);

if($row > 0){
    $_SESSION['usuario'] = $usuario;
    header('Location: principal.php');
    exit();
}else{
    $_SESSION['não_autenticado'] = true;
    header('Location: index.php');
    exit();
}
  • What error is happening? Enter the first if?

  • the error is that it does not log in, when I inform the user and password that is registered in the Database, it goes back to login screen and stays like this, I already cleared the browser cache more

  • session_start is missing, sessions only work with session_start

  • In case you’re entering the first if (empty($_POST['usuario']) || empty($_POST['senha'])){ the data may be arriving via get $_GET by forgetting the definition of the method="post" attribute in the tag <form>.

1 answer

3


session_start is missing, sessions only work with session_start, so:

<?php
include('conexao.php');
if (empty($_POST['usuario']) || empty($_POST['senha'])){
    header('Location: index.php');
    exit();
}

$usuario = mysqli_real_escape_string($conexao, $_POST['usuario']);
$senha = mysqli_real_escape_string($conexao, $_POST['senha']);

$query = "SELECT * from cad.usuario where usuario = '{$usuario}' and senha = '{$senha}'";

$result = mysqli_query($conexao, $query);

$row = mysqli_num_rows($result);

session_start();

if($row > 0){
    $_SESSION['usuario'] = $usuario;
    header('Location: principal.php');
    exit();
}else{
    $_SESSION['não_autenticado'] = true;
    header('Location: index.php');
    exit();
}

All pages that need to be "logged in", after the login of course, will have to have session_start at the beginning, something like:

<?php
session_start();
  • Try here anyway Don’t log in with the user I have in the bank I don’t know what I do anymore

  • @LUIZAUGUSTOSURF what var_dump(mysqli_num_rows($result)); returns? PROBABLY the error is in your other pages that also do not use session_start, session_start has to be in all pages that need login

  • I want to thank all of you for the tips and for me help I got Thank you very much It worked out Here Thank you very much to everyone who will have a little time to help

  • @LUIZAUGUSTOSURF was the session_start that was missing? If yes mark the answer as correct please ... if you do not formulate a response to how you resolved to help future visitors

  • Yes it was the Session_start that was flatando in some page Thank you very much

Browser other questions tagged

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