How can i store session in database usign cakephp 5.6


#1

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


#2

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.


#3

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 file.as 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.

#4

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.

AppController.php

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"


#5

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


#7

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( '127.0.0.1:11211' // 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.