Login Session in an Application

Asked

Viewed 28 times

0

Good Afternoon Guys, I am making a system for company where I work but I have a problem that I can not solve.

This is the Code I’m using:

Code to show user and position

require('conBanco.php'); #conexão com o Banco de Dados
if(isset($_POST['UsrEmail']) && isset($_POST['UsrSenha'])){ #Caso o usuário esteja logando
    $queryUser1 =  mysqli_query($mysqli, "SELECT * FROM usuarios WHERE UsrEmail = '$_POST[UsrEmail]' AND UsrSenha = '$_POST[UsrSenha]' LIMIT 1");
    if(mysqli_num_rows($queryUser1) != 0){
        if($queryUser = mysqli_fetch_assoc($queryUser1)){
            $_SESSION['UserLogado'] = $queryUser['UsrID']; #Salva em uma variável de Sessão a ID do usuário que está logado
            header('Location: login.php');
        }else{
            echo $LoginErro;
            
        }
    }else{
        echo $LoginErro;
    }
}else{ #Caso o usuário já esteja logado
    $QueryLogado = mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT UsrID, UsrNome, UsrSenha, UsrEmail, UsrNvl, NvlNome FROM usuarios INNER JOIN usuarionivel ON UsrNvl = NvlID;"));
    echo "Bem vindo <b>".$QueryLogado['UsrNome']."</b>, tudo bem?<br>Não esqueça que você é <b>".$QueryLogado['NvlNome']."</b> nesta Empresa!";
    echo '<br><a href=../config/Deslogar.php>Sair</a>';
}

The problem is this: I want to show the user his name and his position in the company however, whenever I log in with any different user, with different emails it only shows the name of the ADMINISTRATOR.

someone knows where I might be missing ?

  • A comment: Are you, at any time, initiating the session with the session_start() method? To store the data in the $_SESSION global, you must do this before. I ask this because when the user is logged in, which parameter do you use to recover it? Apparently none. Always return the same: the administrator.

  • I couldn’t quite understand the part of which parameter I’m using, and yes I’m starting the session on the page and capturing the data in the code above in the variable $queryuser.

  • In the situation where the user is already logged in, below the comment: "#If the user is already logged in", you do not pass any parameter to query that indicates which user should be returned, so the returned should always be the "Administrator". Add a parameter in the query to return the user according to the session.

1 answer

-2

Good evening by what I understood man the error should be in the database. picks up the query mounted with different email that you are testing and tests directly into the database to see if it is returning right.

because you said the error only persists when logging in with another user.

  • Your answer does not solve the problem. It would be better converted into commented on the question...

  • I’m sorry but I don’t remember asking your opinion on anything?

  • Good afternoon Buddy, at the bank he does exactly the same thing, I need to know exactly where I’m going wrong, it’s probably in the same code.

Browser other questions tagged

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