Change session expiration time dynamically - Code Igniter 3

0

How do I set a config.php configuration item before it was loaded? I need to change the $ config ['sess_expiration'] item, according to the user.

I tried the following: User.php

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;

Printing to 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\
    )

)
    
asked by anonymous 13.12.2018 / 20:36

1 answer

1

Hello! You do not change $config['sess_expiration'] because this will change the setting globally, even for other variables unrelated to the group that manages the session of a particular user.

For your needs, CodeIgniter has a class called tempdata . It is equal to userdata , which you are already using to save the logged in user, but in 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");
    
26.12.2018 / 17:32