0
Hello! In the restricted page of my site, you need to present the name of the logged-in user, the same occurs in the form that is displayed. But I noticed you’re not showing up, not on the "Hello" or the form. I analyzed the code and couldn’t find anything different than what I had left before. I would like a little help to identify the possible mistake of why not present. :)
This is how it is on the restricted page to search and display the user name on screen:
Olá, <p id="usuario"></p>
<script>document.getElementById("usuario").innerHTML = localStorage.getItem("usuario");</script>
And so it is in the login check code, where I search for the i'ds presented 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 the database.
What are the properties returned by the function
login($dados_usuario['usuario'], $dados_usuario['password'])
? For in a moment you wear$_SESSION['usuario'] = $validacao->user;
and in another you use<?php echo $validacao->usuario?>
. It may be that the property$validacao->usuario
does not exist and yes$validacao->user
.– Carlos Fernandes
You tested by putting the
;
as I mentioned in my reply ? Where are the variables declared$login e $senha
present in your darling ?– MagicHat
@Magichat I tested yes, and did not influence anything. About 2 months ago, the presentation of the user’s name appeared normally without any problem. And last week I went through the site archives and that’s where I saw it no longer presents.
– Inez Boldrin
Okay and the variables ?
– MagicHat
@Magichat Online " Function login($login, $password) {"
– Inez Boldrin
I know it sounds like a detail but the lack of ";" can cause a big headache in the codes I myself have had a hard time with it and looking at the code missing a ";" after: "<? php echo $validacao->usuario". Already gave a var_dump($_SESSION['name_da_sessao']) to see if everything is all right?
– user58118