'CodeIgniter 3 is generating a session file on each request, why?

I use CodeIgniter (v3.1.11) for my webapp and files as session_driver.

The session library is autoloaded and configured like this

$config['sess_driver']                  = 'files';
$config['sess_cookie_name']             = 'ci_session';
$config['sess_expiration']              = 21600;
$config['sess_use_database']            = false;
$config['sess_table_name']              = 'ci_sessions';
$config['sess_match_ip']                = false;
$config['sess_time_to_update']          = 300;
$config['sess_save_path']               = APPPATH . "cache/sessions/";

When I login I set my user informations in the session object using $this->session->set_userdata($myData) And it create the corresponding session file with the informations I need into it

user@:~/path/to/application/cache/sessions$ sudo cat ci_sessionb69ca97bce23674e94809941346fae7300c4afaa
username|s:9:"user";id|i:2;logged_in|b:1;language|s:2:"EN";

But when my front requests the CodeIgniter controllers a new session file is created for each request with empty data and I end up not being able to retrieve the data stored in the initial session file.

user@:~/path/to/application/cache/sessions$ sudo cat ci_sessiondcdd28185aa2f7c9a4f83841193e8c826399abc5
__ci_last_regenerate|i:1603286120;

What could possibly make this happen ?



Solution 1:[1]

because http is an stateless protocol, user tracking will be done with this Aproach that:

Server sends a token to the Client and Client return that token on next request.

this Approach can is Achievable using multiple method:

  • sending an hidden input in html form with token value.
  • set a cookie with token value

and Session* Mechanism using the cookie method.

initially server sets a session_id in httpResponse's cookie. and client return the session_id with each httpRequest.

so your front needs to set the cookie (session_id) on its request, and send it to server in each request.

to prevent server to initialize new session_id and session file for each http request.

Solution 2:[2]

set $config['sess_time_to_update'] = 0; or $config['sess_time_to_update'] = $config['sess_expiration'];

This option controls how often the session class will regenerate itself and create a new session ID. Setting it to 0 will disable session ID regeneration.

Solution 3:[3]

Solve One: Previously you can try that, put the session library name into the autoloader configuration, in application/config/autoload.php

$autoload['libraries'] = array('session');

Solve Two: You can use database sessions.

$config['sess_driver']                  = 'database';
$config['sess_cookie_name']             = 'ci_session';
$config['sess_expiration']              = 21600;
$config['sess_use_database']            = false;
$config['sess_table_name']              = 'ci_sessions';
$config['sess_match_ip']                = false;
$config['sess_time_to_update']          = 300;
$config['sess_save_path']               = APPPATH . "cache/sessions/";

Also, you need to create that table in your database.

CREATE TABLE `sessions` (
  `id` varchar(40) NOT NULL,
  `ip_address` varchar(45) NOT NULL,
  `timestamp` int(10) UNSIGNED NOT NULL DEFAULT '0',
  `data` blob NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT;

Solve Three: If you want, you can use the below code for stop session records.

Create a file application/libraries/Session/MY_Session.php

<?php                                                                                                                                                                                           
defined('BASEPATH') OR exit('No direct script access allowed');

class MY_Session extends CI_Session {

    public function __construct(array $params = array())
    {   
        if ( $this->ignore_sessions() )
            return;
        parent::__construct();
    }   

    function ignore_sessions()
    {   
        $uri = str_replace ("//", "/", $_SERVER['REQUEST_URI']);
        if ( strpos($uri, '/ignore_this_controller/') === 0 ) 
            return true;
        return false;
    }   
}

Also, you can add 'session' to your config/autoload.php

$autoload['libraries'] = array('session')

Solution 4:[4]

If you are using manifest.json in your project, try:

Instead of:

<link rel="manifest" href="manifest.json">

use:

<link rel="manifest" href="manifest.json" crossorigin="use-credentials">

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Abilogos
Solution 2 WinterSilence
Solution 3
Solution 4 LetterC