1
votes

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

3
What is the output of the MySQL command DESC ci_sessions ? It is trying to run the query but telling you no such column session_id exists which it needs for its session storage. Does not sound like the session table is properly defined. - drew010
@drew010, the official table definition for the ci_sessions table does not call for a session_id column. Further, I can store session data if I use the new $_SESSION superglobal, but not with the old method that is supposed to still work - user45623
See my answer below about why it is using session_id. That probably means the session code being used is not using the latest CI3 session library. If you look at the code of CI2 session it uses session_id, CI3 sessions uses id, so the library code being run is CodeIgniter2 not 3 hence the sql error. - drew010
@halfer After the question being on hold and seeing the idiotic typo I made in the very title, I became embarrassed enough to delete the question entirely. However, I've just undeleted it for you. Thanks for your assistance. - user45623

3 Answers

2
votes

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.

1
votes

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

0
votes

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.