Display username logged on screen

0

Hello! In the restricted page of my site, you need to display the username of the logged in user, the same occurs on the form that is displayed. But I noticed that it is not appearing, neither in the "Hello" nor in the form. I looked at the code and could not find anything different from what I had left earlier. I would like a little help to identify the possible error of why not present. :)

This is how you are in the restricted page to search for and display the screen name of the user:

Olá, <p id="usuario"></p>
<script>document.getElementById("usuario").innerHTML = localStorage.getItem("usuario");</script>

And so it's in the verify login code, where I look for the i'ds displayed in the database:

 <?php
    session_start();
    include_once 'config.php';
    ini_set('display_errors',true);
    ini_set('display_startup_erros',1);
    error_reporting(E_ALL);
    if ( isset( $_POST ) && ! empty( $_POST ) ) {
        $dados_usuario = $_POST;
    } else {
        $dados_usuario = $_SESSION;
    }
    $validacao = login($dados_usuario['usuario'], $dados_usuario['password']);
    if(isset($dados_usuario)){
        if ($validacao) {
            $_SESSION['logado'] = true;
            $_SESSION['nome_usuario'] = $validacao->user_name;
            $_SESSION['usuario'] = $validacao->user;
            $_SESSION['user_id'] = $validacao->user_id;
            $_SESSION['nom_clin']= $validacao->nom_clin;
            ?><script>localStorage.setItem("usuario", "<?php echo $validacao->usuario?>");</script><?php
  }
       exit;
        }
     else {
         echo 'Login e Senha incorretos. Favor voltar a página e tentar novamente.';
        // Continua deslogado
        $_SESSION['logado'] = false;
        // Preenche o erro para o usuário
        $_SESSION['login_erro'] = 'Usuário ou senha inválidos';
        //session_destroy();
        //header("Location: restrito.php");
        //exit;

    }

        function login($login, $senha)
        {

            try {
                $sql = "SELECT cod_clin, user, user_name, user_id, nom_clin FROM usuarios WHERE user_id='$login' AND user_password='$senha' LIMIT 1";
                $conn = getConexao();
                $result = $conn->query($sql);
                $row = $result->fetch(PDO::FETCH_OBJ);
                return $row;

            } catch (PDOException $e) {
                echo $e;
                return false;
            }
        }
?>

And the id's match that of the database.

    
asked by anonymous 21.02.2017 / 12:26

4 answers

2

PHP SESSION

You have stored the user name in SESSION so just call it in the code, change it as follows:

Olá, <p id="usuario"><?php echo $_SESSION['nome_usuario']; ?></p>
// Pode remover o Script

To access the SESSION data, it is important to start the Session, using session_start() , as follows:

<?php session_start(); // é possível inicializar o SESSION logo no início do arquivo.
    
21.02.2017 / 12:30
1

Do not forget to log in:

$validacao = login($dados_usuario['usuario'], $dados_usuario['password']);
if(isset($dados_usuario))
{    if($validacao)
     {    session_start();
          //continue

And then you can call it as in Thiago's answer ...

Olá, <p id="usuario"><?php echo $_SESSION['nome_usuario']; ?></p>

Another detail is that the way it is in your question

<?php echo $validacao->usuario?>

A ; is missing, so if it is done with js, start by changing to:

<?php echo $validacao->usuario;?>
------------------------------^
    
21.02.2017 / 12:39
0

You could create an HTTP request method for the code to return a JSON containing all the data you want to use, and pass it to Javascript using Jquery.

$.getJSON("DadosDoUsuario.php").
done(function (dados) {
$("#nomedadiv").html("Olá "+dados.NOMEUSUARIO);
});
    
21.02.2017 / 15:10
0

Hello,

Following query $sql = "SELECT cod_clin, user, user_name, user_id, nom_clin FROM usuarios WHERE user_id='$login' AND user_password='$senha' LIMIT 1"; PDO will return you the following properties: cod_clin , user , user_name , user_id , nom_clin .

object(stdClass) {
    public $cod_clin;
    public $user;
    public $user_name;
    public $user_id;
    public $nom_clin;
}

That is, calling $validacao->usuario will return that the usuario property was not set, breaking the application. Try calling $validacao->user

    
21.02.2017 / 20:22