Problem with session Codeigniter error

0

I am having a small login problem with codeigniter, after logging in it does not transfer the data through the session. I did the following tests:

  • I printed the session on the controler [ok, printed correct data]
  • I printed the session in another controler [did not print anything]
  • I cleaned Cache
Login:
public function login(){
        $usuario    =   $this->input->post('usuario'); // recebe name usuario pelo post
        $senha      =   $this->input->post('senha');
        $this->db->where('usuario', $usuario); // pega o valor igual ao usuario do post no banco
        $this->db->where('senha', $senha);
        $this->db->where('ativo',1);
        $usuario = $this->db->get('usuarios')->result();
        if (count($usuario)===1) {
            $dados  = array(
                'usuario'=>$usuario[0]->usuario,
                'logado'=> TRUE 
                );
            $this->session->set_userdata($dados);
            //print_r($dados);
            redirect('administracao/categorias');
        }else{
            echo heading('Usuario não encontrado', 2);
        }
    }

Login verification:

class Categorias extends CI_Controller{
    public function __construct(){
        parent::__construct();

        if (!$this->session->userdata('usuario') || !$this->session->userdata('logado')) {
            redirect('administracao/home');
        }

    }
    
asked by anonymous 16.12.2015 / 03:12

1 answer

2

Well, I'm not sure if it's the solution, but try recording session by session in your code. Always check if you are calling the session library on your controller or your autoload.php .

I updated the code, making it more correct

Login:

public function login(){
        $usuario    =   $this->input->post('usuario'); // recebe name usuario pelo post
        $senha      =   $this->input->post('senha');
        $this->db->where('usuario', $usuario); // pega o valor igual ao usuario do post no banco
        $this->db->where('senha', $senha);
        $this->db->where('ativo',1);
        $usuario = $this->db->get('usuarios');
        if ($usuario->num_rows() > 0) {

            $this->session->set_userdata('usuario', $usuario);
            $this->session->set_userdata('logado', TRUE);

            //print_r($dados);
            redirect('administracao/categorias');
        }else{
            echo heading('Usuario não encontrado', 2);
        }
    }

Also check your config.php if session settings look something like this:

$config['sess_cookie_name']     = 'ci_session';
$config['sess_expiration']      = 7200;
$config['sess_expire_on_close'] = FALSE;
$config['sess_encrypt_cookie']  = FALSE;
$config['sess_use_database']    = FALSE;
$config['sess_table_name']      = 'ci_sessions';
$config['sess_match_ip']        = FALSE;
$config['sess_match_useragent'] = TRUE;
$config['sess_time_to_update']  = 300;
    
16.12.2015 / 03:34