is as follows. I'm having a problem using $ _SESSION in PHP. I'm starting now, so I'm sorry if I did not login in the right way.
I have a Login screen that basically runs this code:
<?php
session_start();
include_once('classes/Database.php');
if (isset($_SESSION['logged_in'])) {
// Abre a página system.php - Outra tela, onde o nome deve aparecer.
} else {
if (isset($_POST['form_email'], $_POST['senha'])) {
$email = $_POST['form_email'];
$password = md5($_POST['senha']);
$nome = "";
if (empty($email) or empty ($password)) {
$error = "Todos os campos são obrigatórios!";
} else {
$query = $pdo->prepare("SELECT * FROM usuarios WHERE u_email = ? AND u_senha = ?");
$query->bindValue(1, $email);
$query->bindValue(2, $password);
$query->execute();
$num = $query->rowCount();
if ($num == 1) {
// Pega o nome do usuário que logou no sistema
$stmt = $pdo->prepare("SELECT u_nome AS nome FROM usuarios WHERE u_id = '$num'");
$stmt->bindValue(1, $nome);
$stmt->bindValue(1, $num);
$stmt->execute();
$linha = $stmt->fetch(PDO::FETCH_ASSOC);
$nome = $linha['nome'];
$_SESSION['nome'] = $nome;
$_SESSION['logged_in'] = true;
echo $_SESSION['nome'];
header('Location: system.php');
exit();
} else {
$error = 'Email ou senha incorretos!';
}
}
}
?>
// Aqui mostra o código html da página de login
<?php }
Following the structure of the code, you notice that if you found the user in the database I get the variable num, step as the ID and select the name of the user, if I take out header('Location: ...')
I will be on the screen and show the user name, works perfectly. See:
Showthecorrectnameof$_SESSION['nome']
.Butstartingatthepageofsystem.php
thesessionsimplyseemstobedeletedandgivesvariableerrornotdefined.
System.php Code
<?php
session_start();
include_once('classes/Database.php');
echo $_SESSION['nome']; // Aponta erro aqui, lógico
require_once './page_structure.php';
?>
// Aqui mostra o código html da página system.php
If you can help me, the idea is to show a type Welcome followed by the username.
Note: $_SESSION['logged-in']
shows normal in system.php, with value = 1.