user45623
user45623

Reputation: 621

CodeIgniter 3 session confusion

Working on upgrade to CodeIgniter 3, and encountering some issues with the Session class.

We are configured for storing the session in the database. We have set up the proper ci_sessions table and have this in Config.php:

$config['sess_driver'] = 'database';
$config['sess_cookie_name'] = 'ci_session';
$config['sess_expiration'] = 600;
$config['sess_save_path'] = 'ci_sessions';

Trying to store session data with $this->session->set_userdata($data);

With the above we get an error indicating CI is not reading the table name from the "sess_save_path" variable:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SET last_activity = 1437070556, user_data = 'a:14:{s:9:\\"user_data\\";s:0:\\"\' at line 1

UPDATE SET last_activity = 1437070556,

As you can see the table name is missing between UPDATE and SET

To fix this, I went into Config.php and added the old deprecated method of setting the table name that you aren't supposed to use anymore:

$config['sess_table_name'] = 'ci_sessions';

This works better, but I get a new error:

A Database Error Occurred

Error Number: 1054

Unknown column 'session_id' in 'where clause'

UPDATE ci_sessions SET last_activity = 1437075587, user_data = 'a:14:{ [SESSION DATA REMOVED FROM EXAMPLE] }'

WHERE session_id = '636c6c7389342a7a21111e3e2c3c3f03'

Filename: libraries/Session.php

Line Number: 306

What is going on and how do I fix it? Why doesn't $config['sess_save_path'] = 'ci_sessions'; work? What's the deal with the session_id field?

EDIT:

After more testing, I conclude:

$config['sess_save_path'] = 'ci_sessions'; doesn't work

$this->session->set_userdata($data); doesn't work

$this->session->set_userdata('name', $data); doesn't work

$_SESSION['name'] = $data works

Is the CI3 documentation horribly inaccurate, or is something else wrong?

EDIT 2:

New additional problem:

$this->load->library('session');
$data = $_SESSION['data'];

throws Undefined variable: _SESSION. My initial impression is that, in any given function, I have to write to $_SESSION before I can read from $_SESSION

Upvotes: 1

Views: 2643

Answers (3)

zuhair
zuhair

Reputation: 157

I faced this problem after moving form codeigniter 2 to 3. I solved it with replacing "system" folder with the new one, which comes with CI3

Upvotes: 1

Philip V
Philip V

Reputation: 1

I had this same problem and solved it by deleting the system folder and replacing it with the original from the CI3 zip. I overwrote it previously due to an upgrade but still had this error.

Upvotes: 0

drew010
drew010

Reputation: 69957

Something strange is going on with the code.

According to the CodeIgniter3 docs for the session database configuration, the session table needs to be initialized with the following structure:

CREATE TABLE IF NOT EXISTS `ci_sessions` (
        `id` varchar(40) NOT NULL,
        `ip_address` varchar(45) NOT NULL,
        `timestamp` int(10) unsigned DEFAULT 0 NOT NULL,
        `data` blob NOT NULL,
        PRIMARY KEY (id),
        KEY `ci_sessions_timestamp` (`timestamp`)
);

Based on your error the session_id column does not exist which is not what CodeIgniter3 is using. Is your code somehow still loading the CI2 session code? I downloaded the latest version of CI3 and in the Session_database_driver::write() function it makes the following call:

        $insert_data = array(
            'id' => $session_id,
            'ip_address' => $_SERVER['REMOTE_ADDR'],
            'timestamp' => time(),
            'data' => ($this->_platform === 'postgre' ? base64_encode($session_data) : $session_data)
        );

As you can see there is no trace of the session_id column.

Upvotes: 2

Related Questions