Leandro Soares
Leandro Soares

Reputation: 21

My session always loses data when redirecting

Every time I login, in the model the session receives all the data correctly, but when the page is redirected to the home, the session data disappears.

I'm using codeigniter, and this is the model code:

    <?php
    defined('BASEPATH') OR exit('No direct script access allowed');

    class Login extends CI_Controller {
    
    public function __construct()
    {
        parent::__construct();
        if (isset($_SESSION['id_usuario']))
           redirect(base_url());
        $this->load->model('blogueiro_model');
        $this->load->model('pagina_model');
        $this->load->library('form_validation');
    }
    
    public function index()
    {
        $this->load->view('login.php');
    }
    
    public function checa()
    {
        if ( ! $this->input->post())
            show_404();
        
        $this->form_validation->set_rules('login', 'login', 'required');
        $this->form_validation->set_rules('password', 'password', 'required');
        
        $login = html_escape($this->input->post('login'));
        $senha = html_escape($this->input->post('password'));
        
        if ($this->form_validation->run() === TRUE) 
        {
            $usuario = $this->blogueiro_model->autentica($login, $senha);
            
            if ($usuario !== NULL)
            {
                $this->session->set_userdata('id_usuario', $usuario->id);
                $this->session->set_userdata('nome_usuario', $usuario->nome);
                $this->session->set_userdata('login_usuario', $usuario->login);
                $this->session->set_userdata('e_admin', $usuario->e_admin);
                $this->session->set_userdata('foto', $usuario->foto);
                $permissoes = ($usuario->e_admin == 0) ? $this->blogueiro_model->busca_permissoes($usuario->id) : $this->blogueiro_model->lista_todas_permissoes();
                $this->session->set_userdata('permissoes', $permissoes);
                $this->blogueiro_model->limpa_trials($login);

            }
            else
            {
                $this->blogueiro_model->incrementa_trials($login);
                $this->index();
                
            }
        }
        else
        {
            $this->index();
        }
        redirect(base_url());
    }
}

Another thing I noticed was that the status code is always between 302 and 303, I researched a lot but found no solution to this problem.

Follow the status, preview and request images.

Status Code: 302 found

Preview

Request

Upvotes: 2

Views: 70

Answers (1)

Gazmend Sahiti
Gazmend Sahiti

Reputation: 438

Are you using Ci3 ? Are you loading the session library in the controller that has the method that shows the "home".

You can load the library in the controller or methods like this: $this->load->library('session'); or you just can auto load the session library everywhere adding the session library in application/config/autoload.php.

For more please check Auto loading in CodeIgniter 3

Upvotes: 2

Related Questions