Codeigniter session expiring after an action

Asked

Viewed 984 times

6

I made an administrative panel using CodeIgniter and every time the session is expiring, thus redirecting to the login screen. Every time for example, that I edit a category, after edited and click on another menu link, the system redirects to 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 have to exist, because it is very fast this "expiration" session.

The link from the menu http://localhost/RP/admin/banners

user_model.php (file responsible for login)

<?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'));
    }
}

categorias_model.php (model responsible for the 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>';
    }
}
  • Hello, you could show more parts of the code, as the login part, edit category and a method of any menu link, because only analyzed this cofiguration snippet do not give to know, since it is correct and setting the duration 2 hours for the sessions, this is defined in the following line: $config['sess_expiration'] = 7200;//seconds

  • @Yurepereira put in the publication

  • CI give up is nothing new, hehehehehe

  • 1

    He’s falling in the: if(!$sessao AND empty($sessao)) redirect(base_url('login')); or no: redirect(base_url('login'));? Echo your $this->session->userdata('user_id'); on your view, to see if he’s keeping the value. - Just an Obs, returning HTML in a model is bad practice, you should use HTML only in views.

2 answers

1

To try to solve this problem try to increase the time the sessions are updated in the configuration variable sess_time_to_update, thus:

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

Also check the cookie settings in the browser where you are testing your system, as Codeigniter uses cookies to record sessions created with it.

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 stated in the documentation, it is easier and you can through this galley generate saved logs and user control.

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

https://www.codeigniter.com/user_guide/libraries/sessions.html?highlight=session

Browser other questions tagged

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