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.