Problem with Codeigniter error session

Asked

Viewed 691 times

1

I have a small login problem with codeigniter, after logging in it does not transfer the data by session.
I took the following test:

  • I printed the session in the controler [ok, printed correct data]
  • I printed the session on another controller [printed nothing]
  • cleared 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');
        }

    }
  • 2

    Could put the source code?

  • @rray Update with the code

1 answer

2

Well, I’m not sure if it’s the solution, but try to record session by session in your code. Always make sure you’re calling the library session on your controller or on your controller autoload.php.

I updated the code, making it more accurate

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 are more or less 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;
  • I was able to solve the problem, I just cleared the browser history and went back to normal

Browser other questions tagged

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