0

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');
        }
    }
10
  • Try also posting in pt.stackoverflow.com Commented May 6 at 0:14
  • Hi, I've just translated it into English, I hadn't realised before. So, I was using var_dump to find out if the session was started or not and it's not working on the redirect after filling in the login form. Commented May 6 at 0:59
  • Why do you expect $_SESSION to not be empty, before you even out anything in it? Commented May 6 at 1:48
  • I may have got confused when trying to start the session, as the intention is to make it initialise itself after logging in, so I need to update the function Commented May 6 at 1:56
  • 1
    Where is your 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 the var_dump() currently in your login() will prevent the redirect from working. Commented May 6 at 5:16

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.