Question:
I made an admin panel using CodeIgniter
and all the time the session keeps expiring, thus redirecting to the login screen. Every time, for example, I edit a category, after I edited it and click on 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 don't know if these settings have to be, as this session "expiration" is very fast.
The menu link 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'));
}
}
categories_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>';
}
}
Answer:
To try to solve this problem, try to increase the time at which the sessions are updated in the sess_time_to_update
configuration variable, like this:
$config['sess_time_to_update'] = 86400;// 24 horas
It also checks the cookie settings on the browser where you are testing your system, as Codeigniter uses cookies to record sessions created with it.