Codeigniter session expiring after an action

6

I made an administrative panel using CodeIgniter and every time the session expires, thus redirecting to the login screen. Every time, for example, that I edit a category, after editing it and clicking another menu link, the system redirects to the login screen again.

config.php (CodeIgniter default file)

$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 do not know if these settings exist, since this "session expiration" is very fast.

The link menu link

user_model.php (login file)

<?php
class User_model extends CI_Model
{

    public function __construct()
    {
        parent::__construct();
    }

    public function DadosUser()
    {
        $sessao = $this->session->userdata('user_id');

        if (!$sessao AND empty($sessao)) {
            redirect(base_url('login'));  
        } 

        $this->db->where('id', $sessao);
        $user = $this->db->get('admin_users');

        if ($user->num_rows() > 0) {
            return $user->row();
        }

        redirect(base_url('login'));
    }
}

categories_model.php (model responsible for category)

<?php
class Categorias_model extends CI_Model
{
    public function __construct()
    {
        parent::__construct();
    }

    public function NovaCategoria()
    {
        $config['upload_path'] = '../uploads/banners';
        $config['encrypt_name'] = true;
        $config['allowed_types'] = 'png|gif|bmp|jpg|jpeg|pjpeg';

        $categoria = $this->input->post('categoria');
        $ordem = $this->input->post('ordem');

        $query = $this->db->query("SELECT (MAX(codigo_categoria) + 1) AS codigo FROM Categorias");
        $novoCodigo = $query->row()->codigo;

        $data = array(
            'codigo_categoria' => $novoCodigo,
            'nome_categoria' => $categoria,
            'ordem' => $ordem
        );

        if (!empty($_FILES['userfile']['tmp_name'])) {

            $this->upload->initialize($config);

            if ($this->upload->do_upload()) {

                $upload = $this->upload->data();

                $data['imagem'] = $upload['file_name'];
            }
        }

        if ($this->db->insert('Categorias', $data)) {
            return '<div class="alert alert-success text-center">Categoria cadastrada com sucesso!</div>';
        }

        return '<div class="alert alert-danger text-center">Erro ao cadastrar categoria.</div>';
    }
}
    
asked by anonymous 21.11.2015 / 21:00

2 answers

1

To try to resolve this issue, try to increase the time the session update is done in the sess_time_to_update configuration variable, like this:

$config['sess_time_to_update'] = 86400;// 24 horas

It also checks the cookie settings in the browser where you are testing your system, since Codeigniter uses cookies to record the sessions created with it.

    
23.11.2015 / 13:31
0

You have to change in application > config > config.php

$config['sess_time_to_update']  = 172800; // 48 horas por exemplo

Try user sessions using database as it appears in the documentation, it is easier and you can through this gallery generate logs and user control.

Documentation link, remembering that codeigniter is already in version 3.1 (at the time of this response):

link

    
22.02.2017 / 15:04