Session is destroyed after redirect in Codeigniter 3

Asked

Viewed 307 times

1

Developing a login system, I wrote the following script in the controller:

    $usuario = $this->input->post('usuario');
    $senha = $this->input->post('uenha');
    $q = $this->login->logar($usuario, $senha);
    if (!$q):
        $this->session->set_userdata('logado',false);
        redirect('login');
    endif;
    $this->session->set_userdata('logado',true);
    redirect('redacoes');

This is an example.

It turns out that after redirect to another controller, sessions are not being stored.

Note: I clicked on autoload.

  • Yes, I am! I even made it clear in the question. Any session I declare in controller X and then redirect, or even manually access in controller Y, disappears in a magic touch..

1 answer

2


Try it this way:

$logar = $this->login->logar($this->input->('usuario'), $this->input->post('senha'));

if($logar){
    $this->session->set_userdata('logado', "OK");
    redirect('redacoes');
} else {
    $this->session->set_userdata('logado', false);
    redirect('login');
}
  • Note the password field, your question is written uenha, could be that?

  • 1

    Can you detail what is different? even because the problem is not in my model, because it returns true or false correctly.

  • Very unlikely.. I rewrote the same code several times and suddenly I wrote the question wrong.

  • On your Session check, you do how? if($this->Session->userdata('logged in')) echo "logged in"; ?

  • I’m restricting access to the page. But the problem goes away from the check, because I’m leaving a var_dump, and when I update from one page to another all sessions disappear, except for the codeigniter pattern.

  • Uhmmmm, your return gives as true?

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.