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
– Yure Pereira
@Yurepereira put in the publication
– Alisson Acioli
CI give up is nothing new, hehehehehe
– Wallace Maxters
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.– wmarquardt