Hax0r
Hax0r

Reputation: 1823

How to control sessions in Codeigniter

I am using Codeigniter3 and I am trying to generate session only for specifically allowed users (the users of our service).

Because codeigniter 3 is creating sessions for all requests, the number of sessions stored in our database grows so rapidly. And I want to keep minimum number of sessions as possible.

The configuration for session is like below.

$config['sess_driver'] = 'database';
$config['sess_cookie_name'] = '_s';
$config['sess_expiration'] = 0;
// $config['sess_save_path'] = NULL;
$config['sess_save_path'] = 'CiSessions';
$config['sess_match_ip'] = FALSE;
$config['sess_time_to_update'] = 30000;
$config['sess_regenerate_destroy'] = FALSE;

Upvotes: 1

Views: 81

Answers (1)

Atural
Atural

Reputation: 5439

i guess in Codeigniter this is impossible the only thing i can imagine is the use of cookies

Meaning:

On your login Page you set a cookie like

$this->load->helper("cookie");
$cookie = array(
    'name'   => 'isLoggedIn',
    'value'  => '1',
    'expire' => '7200',
    'path'   => '/'
);
$this->input->set_cookie($cookie);

in your autoload.php

you simply load this library only if the cookie is set

$arrAutoloadLibraries = ["database","user_agent"];
if ($_COOKIE['isLoggedIn'] == 1)
{
    $arrAutoloadLibraries[] = "session";
}

$autoload['libraries'] = $arrAutoloadLibraries;

Be aware there are some logical issues you face with (what happens if user just visits the login page and leaves after that, what happens on failed login attempt, etc.) So you've to think about these possibilities.

I don't like this approach, but as i said that's the only thing, which came to mind ;)

Upvotes: 1

Related Questions