Dynamically change session expiration time - Code Igniter 3

Asked

Viewed 336 times

0

Like me Seto a config.php file configuration item before he was loaded? I need to change the item $config['sess_expiration'], according to the user.

I tried it this way: Php user.

public function logar() {
    $login                 = addslashes(strtoupper($this->input->post("login")));
    $senha                 = addslashes(md5($this->input->post("senha")));
    $this->dados['login']  = $this->usuarios->getUser($login, $senha);
    $logged                = $this->dados['login'][0]->LOGGED;

    if (count($this->dados['login']) > 0) {

        if($this->dados['login'][0]->AUTORI == 1 || $this->dados['login'][0]->AUTORI == '1') {
            $this->session->set_flashdata('erro_login', 'Este usuário não está ativo no banco configurações!');
            redirect('');   
        }

        $sess[0] = (object) array(
            'idusua'      => $this->dados['login'][0]->IDUSUA,
            'nomusu'      => $this->dados['login'][0]->NOMUSU,
            'tokusu'      => $this->dados['login'][0]->TOKUSU,
            'sesexp'      => $this->dados['login'][0]->SESEXP,
            'logou_token' => false,
            'anomes'      => date('Ym'),
            'conusu'      => json_decode($this->dados['login'][0]->CONUSU),
            'condbu'      => array(
                'server' => $this->encryption->encrypt($this->dados['login'][0]->SERVER),
                'user'   => $this->encryption->encrypt($this->dados['login'][0]->USERRR),
                'pass'   => $this->encryption->encrypt($this->dados['login'][0]->SENHAA),
                'db'     => $this->encryption->encrypt($this->dados['login'][0]->DATBAS)
            )
        );

        $this->config->set_item('sess_expiration', $this->dados['login'][0]->SESEXP); // => nesta linha tentei setar, mas não sobrescreve o valor em config.php 
        $this->session->set_userdata("sessao", $sess);

        if($logged == 'N') {
            $this->usuarios->updateLogged($this->dados['login'][0]->IDUSUA, 'S'); 
            redirect('Principal/');   
        } else {
            $this->session->set_flashdata('erro_login', 'Usuário já está logado em outro dispositivo!');
            redirect('');
        }
    } else {
        $this->session->set_flashdata('erro_login', 'Dados informados inválidos!');
        redirect('');
    }
}

config.php

$config['sess_driver']              = 'database';
//$config['sess_driver']              = 'files';
$config['sess_cookie_name']         = 'ci_session';
$config['sess_expiration']          = 0; // se deixar 0, retorna 0 mesmo, se comentar a var toda, nao retorna nada porque nao existe
$config['sess_expire_on_close']     = TRUE;
$config['sess_regenerate_destroy']  = FALSE;
//$config['sess_save_path'] = APPPATH . 'ci_sessions/';
$config['sess_save_path']           = 'ci_session';
$config['sess_match_ip']            = TRUE;
$config['sess_time_to_update']      = 300;

Printando for debugg:

CI_Config Object (
[config] => Array
    (
        [app_version] => 1.0
        [base_url] => http://192.168.0.30/projetos/dashboard/
        [index_page] => 
        [uri_protocol] => AUTO
        [url_suffix] => 
        [language] => english
        [charset] => UTF-8
        [enable_hooks] => 
        [subclass_prefix] => MY_
        [composer_autoload] => 
        [permitted_uri_chars] => 'a-z 0-9~%.:&_\/-=?+',;()
        [enable_query_strings] => 
        [controller_trigger] => c
        [function_trigger] => m
        [directory_trigger] => d
        [allow_get_array] => 1
        [log_threshold] => 0
        [log_path] => 
        [log_file_extension] => 
        [log_file_permissions] => 420
        [log_date_format] => Y-m-d H:i:s
        [error_views_path] => 
        [cache_path] => 
        [cache_query_string] => 
        [encryption_key] => PuZDlRnAHX1qdjiBiFwegiVSTzK6XSln
        [sess_driver] => database
        [sess_cookie_name] => ci_session
        [sess_expiration] => 0
        [sess_expire_on_close] => 1
        [sess_regenerate_destroy] => 
        [sess_save_path] => ci_session
        [sess_match_ip] => 1
        [sess_time_to_update] => 300
        [cookie_prefix] => 
        [cookie_domain] => 
        [cookie_path] => /
        [cookie_secure] => 
        [cookie_httponly] => 
        [standardize_newlines] => 
        [global_xss_filtering] => 
        [csrf_protection] => 
        [csrf_token_name] => csrf_test_name
        [csrf_cookie_name] => csrf_cookie_name
        [csrf_expire] => 7200
        [csrf_regenerate] => 1
        [csrf_exclude_uris] => Array
            (
            )

        [compress_output] => 
        [time_reference] => local
        [rewrite_short_tags] => 
        [proxy_ips] => 
    )

    [is_loaded] => Array (
    )

    [_config_paths] => Array(
        [0] => C:\xampp\htdocs\projetos\dashboard\application\
    )

)

1 answer

1


Hello! You should not change $config['sess_expiration'] because this will change the configuration globally, including for other variables not related to the group that manages a particular user’s session.

For your need, Codeigniter has a class called tempdata. Is equal to userdata, that you are already using to store the logged in user, but tempdata you specify how long the variable will last on the server. The time value is in seconds. Example:

$this->session->set_tempdata('nome_objeto', 'valor_objeto', 60 * 5); // 5 minutos

In your case it will be:

$this->session->set_tempdata("sessao", $sess, $this->dados['login'][0]->SESEXP);

And to read the value:

$minha_sessao = $this->session->tempdata("sessao");

Browser other questions tagged

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