Kohana v3 :: Session Database Driver

I was having a little trouble getting the session to work while using the database driver (instead of the Native or Cookie drivers), but I eventually found the error. I was using the sessions table MySql create statement from Kohana 2.3 documentation, which is slightly different from the required table for 3.0. So here is a brief tutorial in using the Session with the database driver.

  1. create the sessions database table (mysql example):
    CREATE TABLE `sessions` (
    `session_id` varchar(127) NOT NULL,
    `last_active` int(10) unsigned NOT NULL,
    `contents` text NOT NULL,
    PRIMARY KEY (`session_id`)
    ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
  2. In the bootstrap.php file, uncomment the line for the database module
  3. Copy the /modules/database/config/database.php file to /application/config/database.php and fill in the appropriate database connection variables
  4. to test, create a controller action and run it:
    public function action_set()
    {
    $session = Session::instance('database');
    $session->set('foo', 'hello from the session db');
    }
  5. then retrieve the session data in another controller action and run it:
    public function action_get()
    {
    $session = Session::instance('database');
    $data = $session->get('foo');
    echo Kohana::debug($data);
    }
  6. You should be greated with our session string: ‘hello from the session db’

Tags: , ,

Leave a Comment