How can i store session in database usign cakephp 5.6

I have done both configuration and created database name as cake.session. but still now i can not store session value in data base.
Anyone can help me

Current CakePHP version is 3.4. There is no version 5.6. Please check your version number. Please share part of your config file and explain your problem in more details.

Thank you for your reply.
And sorry for wrong information about cakephp version.
I am working on 2.x cakephp version.
I have created cake_sessions table as following

CREATE TABLE cake_sessions ( id varchar(255) NOT NULL default ‘’, data text, expires int(11) default NULL, PRIMARY KEY (id));

I have configure in core.php following.

<?php Configure::write('Session', array( 'defaults' => 'database', 'handler' => array( 'model' => 'cake_sessions' ) )); I have created in model like CakeSession.php. But still now its can not store the session in database.

In core.php Configure::write('Session', array('defaults' => 'database')); should be enough.

You do not need app/Model/CakeSesions.php, nor a dedicated Controller for the sessions as Cake does everything nicely without these files.

Than you should add session to your components and helpers.


class AppController extends Controller
public $components = array('Session');
public $helpers = array('Session');
//rest of your code

After this in any controller you can write and read session.

$this->Session->write('vaiableName', 'value');
echo $this->Session->read('vaiableName'); //shold be "value"

Thank you sir for reply.
I will implement and then let you know.

Hello sir,
I have done same configuration but still now it is not storing session data in database.

This code is for my core.php

<?php Configure::write('debug', 2); Configure::write('Error', array( 'handler' => 'ErrorHandler::handleError', 'level' => E_ALL & ~E_DEPRECATED, 'trace' => true )); Configure::write('Exception', array( 'handler' => 'ErrorHandler::handleException', 'renderer' => 'ExceptionRenderer', 'log' => true )); Configure::write('App.encoding', 'UTF-8'); Configure::write('Session', array('defaults' => 'database')); Configure::write('Acl.classname', 'DbAcl'); Configure::write('Acl.database', 'default'); App::uses('MemcachedEngine', 'Memcached.Lib/Cache/Engine'); $engine = 'Memcached'; if (extension_loaded('apc') && function_exists('apc_dec') && (php_sapi_name() !== 'cli' || ini_get('apc.enable_cli'))) { $engine = 'Apc'; } $duration = '+999 days'; Cache::config('_cake_core_', array( 'engine' => $engine, 'prefix' => $prefix . 'cake_core_school_', 'path' => CACHE . 'persistent' . DS, //'serialize' => ($engine === 'File'), 'duration' => $duration, 'lock' => true, # !!! )); Cache::config('_cake_model_', array( 'engine' => $engine, 'prefix' => $prefix . 'cake_model_school_', 'path' => CACHE . 'models' . DS, //'serialize' => ($engine === 'File'), 'duration' => $duration )); Cache::config('default', array('engine' => 'Memcached')); Cache::config('default', array( 'engine' => 'Memcached', //[required] 'duration' => 3600, //[optional] 'probability' => 100, //[optional] 'prefix' => 'cake_school_', //[optional] prefix every cache file with this string 'servers' => array( '' // localhost, default port 11211 ), //[optional] 'persistent' => true, // [optional] set this to false for non-persistent connections 'compress' => false, // [optional] compress data in Memcache (slower, but uses less memory) )); And i have done for appcontroller same configuration. but still now not working.

Hi rrd, is this approach also valid for CakePHP 4.1.4 [the current version]? and cover this. In short, basically yes.

It should.

We have a hungarian facebook group if you are interested :slight_smile:

Thank you, nNo Facebook, no Twitter, no Instagram, no Youtube and nevertheless I am alive :wink:

:slight_smile: Like a psyhopat