CakePHP 2: Not writing session data to database


#1

Is there any reason why a CakePHP 2 project won’t write session data to the database (MySQL)?

As per instructions here I’ve created a cake_sessions table in the DB using:

CREATE TABLE 'cake_sessions' (
  'id' VARCHAR(255) NOT NULL DEFAULT '',
  'data' TEXT,
  'expires' INT(11) DEFAULT NULL,
  PRIMARY KEY ('id')
);

I’ve also modified core.php to reflect the following:

Configure::write( 'Session', [
    'defaults'       => 'database',
    // Modify Session Timeout
    'timeout'        => 1440,
    'cookieTimeout'  => 1440,
    'checkAgent'     => false,
    'autoRegenerate' => true,
] );

However, attempting to load pages produces session errors (failed to read session data) and no data is written to the DB.

I’ve carried out similar steps in CakePHP 3 projects and it works out of the box.

Any help here will be greatly appreciated.


#2

RESOLVED. My version of Cake was 2.8 and upgrading to 2.10.1 fixed the issue. My best guess is DB Sessions wasn’t fully implemented in 2.8?


#3

Your configuration needs to indicate “database” sessions. I didn’t see that in what you posted.


#4

Sorry - posted wrong code! Issue resolved anyways by upgrading to 2.10.x.