蛋糕PHP 2.5.2。从数据库读取会话

Cake PHP 2.5.2. sessions reading from database

当设置为数据库时,我无法从 cake php 会话中读取值,它 returns 为空。

我已经设置在core.php

Configure::write('Session', array(
    'defaults' => 'database'
));

我设置了一个 cakephp 会话 table ,它将会话数据存储在 table.

我试着用

读回来
$pid = $this->Session->read('Projectid');

我用

写的
  $this->Session->write('Projectid', key($projects) );

有什么想法吗? 它在使用 php 会话而不是数据库时有效。

数据库sql

   CREATE TABLE IF NOT EXISTS `cake_sessions` (
     `id` varchar(255) NOT NULL,
   `data` text,
  `expires` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
  ) ENGINE=InnoDB DEFAULT CHARSET=latin1;

更新

我发现如果我删除将列表写入会话的一行,它可以读取其他内容variables.Is这是一个错误吗?

 $projects =   $this->Project->find('list', array('fields' => array('id','name')
$this->Session->write('Projects', $projects); //removing this line i can read

我在使用 CakePHP 2.3 时遇到了同样的问题,无法使用默认数据库配置在 Session 中写入或读取。

然后,我遵循了这个示例,Custom Handler CakePHP Session,但是我无法访问服务器以正确配置 php-apc,所以,我只是从示例中删除了 apc 功能.

我在 app/Model/Datasource/Session/CustomSessionHandler.php 中的最终代码(示例中的 ComboSession)如下所示。

<?php

App::uses('DatabaseSession', 'Model/Datasource/Session');

class CustomSessionHandler extends DatabaseSession implements     CakeSessionHandlerInterface {
    public $cacheKey;

    public function __construct() {
        parent::__construct();
    }

    // read data from the session.
    public function read($id) {
        $result = Cache::read($id);
        if ($result) {
            return $result;
        }
        return parent::read($id);
    }

    // write data into the session.
    public function write($id, $data) {
        Cache::write($id, $data);
        return parent::write($id, $data);
    }

    // destroy a session.
    public function destroy($id) {
        Cache::delete($id);
        return parent::destroy($id);
    }

    // removes expired sessions.
    public function gc($expires = null) {
        Cache::gc();
        return parent::gc($expires);
    }
}

我的 app/Config/core.php 会话配置部分。

Configure::write('Session', array(
  'defaults' => 'database',
  'handler' => array(
      'engine' => 'CustomSessionHandler',
      'model'  => 'Session'
   )
));

最后是我的 table 创建的存储会话。

CREATE TABLE `sessions` (
  `id` varchar(255) NOT NULL DEFAULT '',
  `data` text,
  `expires` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
);

完成这些步骤后,现在我可以读取和写入会话,没有任何问题,我的会话存储在 'sessions' table.

问题是我在 table 中使用日语,但它没有设置为 UTF-8,这导致将会话保存到数据库时出现问题。