2012-02-23 45 views
2

請幫我理解,爲什麼會話不在數據庫中寫入?Kohana 3.2在數據庫中保存會話

會話配置

'database'  => array(
    'name'  => 'session_database', 
    'encrypted' => TRUE, 
    'lifetime' => 43200, 
    'group'  => 'default', 
    'table'  => 'sessions', 
    'columns' => array(
     'session_id' => 'session_id', 
     'last_active' => 'last_active', 
     'contents'  => 'contents' 
    ), 
    'gc' => 500, 
), 

我這樣做:

Session::$default = 'database';  
$this->session = Session::instance();  
$this->session->set('test', 'test'); 

然後我重新加載頁面,我沒有看到表sessions一個新行中DB

+0

在好奇心的情況:你爲什麼打算這樣做呢? – zerkms 2012-02-23 03:13:16

+0

它是否發出任何錯誤? – yoda 2012-02-23 03:14:00

+0

我想保持$ this-> session-> set('test','test');在數據庫中 – 2012-02-23 12:50:56

回答

0

我不不知道你是否這樣做了,但你需要使用這個確切的查詢來創建表格:

CREATE TABLE `sessions` (
    `session_id` VARCHAR(24) NOT NULL, 
    `last_active` INT UNSIGNED NOT NULL, 
    `contents` TEXT NOT NULL, 
    PRIMARY KEY (`session_id`), 
    INDEX (`last_active`) 
) ENGINE = MYISAM; 
+0

exsistCREATE表保存IF NOT EXISTS'sessions'( 'session_id' VARCHAR(24)NOT NULL, 'last_active' INT(10)無符號NOT NULL, 'contents'文字NOT NULL, PRIMARY KEY('session_id'), KEY'last_active'('last_active') )ENGINE = MyISAM DEFAULT CHARSET = utf8; – 2012-02-23 09:46:58

0

也許你的設置Session::$default = 'database';被覆蓋Session::instance()。 試試這個:

$this->session = Session::instance('database'); 
$this->session->set('test', 'test'); 
0

檢查您的日誌中是否有錯誤。

我看到您已設置會話數據進行加密。確保你已經在加密配置文件中定義了一個'密鑰'。您也可以嘗試將數據庫會話的加密設置設置爲false,以查看是否導致您的錯誤。

2

如果您使用的是加密,那麼您需要確保在encrypt.php配置文件中設置了加密密鑰。

'database'  => array(
    'name'  => 'session_database', 
    'encrypted' => TRUE,    /* using encryption requires a key */ 
) 

/application/config/encrypt.php

<?php defined('SYSPATH') OR die('No direct script access.'); 

return array(

    'default' => array(
      /** 
      * The following options must be set: 
      * 
      * string key  secret passphrase 
      * integer mode encryption mode, one of MCRYPT_MODE_* 
      * integer cipher encryption cipher, one of the Mcrpyt cipher constants 
      */ 
      'cipher' => MCRYPT_RIJNDAEL_128, 
      'mode' => MCRYPT_MODE_NOFB, 
      'key' => 'my_encryption_key' 
    ), 

); 
+2

這完全正確,幫了我很多。謝謝。 Kohana的文檔是不正確的 - 沒有檢查,我盲目地命名我的加密配置文件'encryption.php'。 '複製默認的config/encryption.php' - http://kohanaframework.org/3.3/guide/kohana/security/encryption。偉大的框架,可怕的文檔。 – chrisboustead 2014-01-21 16:26:21