I'm trying to authenticate a user and store the data in the session. I expected that the session should contain the user's data after login, but the current behaviour is that var_dump($_SESSION) displays array(0) { } before displaying the correct data.
The functions used are
public function autenticar($email, $senha) {
$usuario = $this->model->buscarPorEmail($email);
if ($usuario && $this->model->validarSenha($email, $senha)) {
$_SESSION['usuario'] = [
'id' => $usuario['id'],
'nome' => $usuario['nome'],
'email' => $usuario['email'],
'tipo' => $usuario['tipo']
];
return true;
}
return false;
}
public function login() {
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$email = $_POST['email'] ?? null;
$senha = $_POST['senha'] ?? null;
if ($this->autenticar($email, $senha)) {
var_dump($this->autenticar($email, $senha));
header('Location: /sebo/public/');
exit;
} else {
echo $this->twig->render('/login.twig', [
'error' => 'E-mail ou senha inválidos.'
]);
}
} else {
echo $this->twig->render('/login.twig');
}
}
$_SESSIONto not be empty, before you even out anything in it?var_dump($_SESSION);happening? Do you have ` session_start();` somewhere? What does "before displaying the correct data" mean, are you seeing 2 sets of output? Note that thevar_dump()currently in yourlogin()will prevent the redirect from working.