Codeigniter 3 session not working

On a web application, session not working. I verified PHP session is working with script

https://gist.github.com/serverok/8c504205ae0357e0c6488eab880a77bf

When refreshing the script, the number start increasing, that confirms PHP session works fine on the server.

As for Codeigniter, we need to check the session settings in file application/config/config.php

$config['sess_driver'] = 'files';
$config['sess_cookie_name'] = 'ci_session';
$config['sess_expiration'] = 7200;
$config['sess_save_path'] = FCPATH.'ci_sessions';
$config['sess_match_ip'] = FALSE;
$config['sess_time_to_update'] = 300;
$config['sess_regenerate_destroy'] = FALSE;

That looks good. Session will be saved in folder

FCPATH.'ci_sessions';

FCPATH will be replaced with web application root folder where index.php is present. I verified ci_session folder exists and changed its permisison to 777, but that did not fixed the session problem.

To verify session is working, i added 2 function to controller

public function set-session()
{
    $newdata = array(
        'username'  => 'johndoe',
        'email'     => '[email protected]',
        'logged_in' => TRUE
    );
    $this->session->set_userdata($newdata);
    die("test");
}

public function read-session()
{
    $this->load->library('session');
    $name = $this->session->userdata();
    echo "
";
    var_dump($name);
    var_dump($_SESSION);
    exit;
}

I can call these functions to verify is session is setting properly with url like yourdomain/controller-name/function-name

The problem was due to PHP 7. To fix it, edit file

system/libraries/Session/Session.php

Find

ini_set('session.name', $params['cookie_name']);

Replace with

ini_set('session.id', $params['cookie_name']);

You can find more about Codeigniter session at https://codeigniter.com/userguide3/libraries/sessions.html

Need help with Linux Server or WordPress? We can help!

One thought on “Codeigniter 3 session not working

Leave a Reply

Your email address will not be published. Required fields are marked *