1

I was trying to build an application but I faced a problem with the session data. So I downloaded a new fresh CodeIgniter project and tried to work with the session but still failed. Here's what I did:

application/config/autoload.php:

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

$autoload['helper'] = array('url');

application/config/config.php:

$config['sess_save_path'] = 'D:\Projects\CodeIgnter-fresh\application\cache\session';

After that I created a controller called User.php

User.php

<?php

class User extends CI_Controller {

    public function index() {
        $this->session->set_userdata('somedata', 'somevalue');
        redirect('user/dashboard', 'refresh');
    }

    public function dashboard() {
        var_dump($this->session->userdata);
    }
}

The controller contains two simple methods. The index method sets some user data and after that redirects to the other method, which dumps the userdata. Unfortunately here's what I see:
enter image description here
It seems the only data I am able to see is some CodeIgniter data, but for sure the data I am trying to set is gone. Interesting thing I noticed was that in application/cache/session folder I can see some files with my data in it. So I guess it has to be some permission problem, but I cannot figure it out. Any suggestions?

I'm using:
OS: Windows 10 Education
PHP version: 7.2.11
CodeIgniter version: 3.1.10

Teddy G.
  • 49
  • 5

2 Answers2

1

I see 2 issues in this question:

  1. not being able to write the session information to the disk using CI file driver
    and
  2. not being able to retrieve the session information

1. write the session information to the disk

The config.php at around line 344 says

'sess_save_path'

The location to save sessions to, driver dependent.

For the 'files' driver, it's a path to a writable directory. WARNING: Only absolute paths are supported!

IMPORTANT: You are REQUIRED to set a valid save path!

Therefore you need to set it either to false (nothing is saved) or use an absolute path. In order to make this work for development and production alike, you can use the PHP $_SERVER array and use it in your config.php like:

$config['sess_save_path'] = $_SERVER['DOCUMENT_ROOT'].'/application/cache/session/';

finally you need to make sure, that above directory is writable, but only you have access to that directory (on production server).

P.S.: you see what's in $_SERVER using this line:

<?php echo'<pre>';print_r($_SERVER); ?>

2. retrieve the session information

  • make sure that you don't destroy the session in a previous function
  • keep in mind changing $config['cookie_domain'], $config['cookie_path'], $config['cookie_secure'] can affect sessions:

this setting works on my setup:

$config['sess_driver'] = 'files';
$config['sess_cookie_name'] = 'ci_session';
$config['sess_expiration'] = 7200;
$config['sess_save_path'] = $_SERVER['DOCUMENT_ROOT'].'/application/cache/session/';
$config['sess_match_ip'] = FALSE;
$config['sess_time_to_update'] = 300;
$config['sess_regenerate_destroy'] = FALSE;

$config['cookie_prefix']    = '';
$config['cookie_domain']    = '';
$config['cookie_path']      = '/';
$config['cookie_secure']    = FALSE;
$config['cookie_httponly']  = FALSE;

If none of this works, you can still try to override autoload, putting this constructor into your User controller:

public function __construct(){
  parent::__construct();
  $this->load->library('session');
}
Vickel
  • 7,879
  • 6
  • 35
  • 56
  • That's correct for sure. But I'm still not getting the session data, the result remains the same.. How can I check the permitions of the folder? – Teddy G. May 29 '19 at 17:10
  • I answered the question, as I thought you cannot store the session on your server using the 'files' driver. I just looked again at your dashboard function. It should be `var_dump( $this->session->userdata() );` – Vickel May 29 '19 at 17:24
  • No matter what I dump: `$this->session->userdata` , `$this->session->userdata()` or even `$_SESSION` , I still get the same result... – Teddy G. May 29 '19 at 17:28
  • but do you see the file in /application/cache/session/ ? – Vickel May 29 '19 at 17:29
  • Yes, I can see it – Teddy G. May 29 '19 at 17:35
  • I'm sorry, I cannot reproduce this on my side, I used your functions and output is: array(8) { ["__ci_last_regenerate"]=> int(1559151567) ..etc... ["somedata"]=> string(9) "somevalue" }; maybe clear cache, or try in another browser? – Vickel May 29 '19 at 17:47
  • one more thing comes in my mind, did you change the default cookie config? – Vickel May 29 '19 at 19:03
0

Set The Driver to 'files' in config:

$config['sess_driver'] = 'files';

Further Information here Codeigniter Session User Manual

Sam Tigle
  • 393
  • 3
  • 14