Syncing sessions between Yii and KCFinder

You are viewing revision #1 of this wiki article.
This version may not be up to date with the latest version.
You may want to view the differences to the latest version.

next (#2) »

I have been trying to find information on how to sync the sessions between Yii and KCFinder so that the two applications can "communicate" with each other. I managed to find some bits and pieces on the correct approach to use, but not a definite guide or concrete code.

So... if you are using Yii with CDbHttpSession and also want KCFinder to share the same session as Yii, you must create your own SESSION SAVE HANDLER as described here.

This handler must be placed inside file core/autoload.php of KCFinder.

class SessionSaveHandler 
{
    protected $savePath;
    protected $sessionName;
    
    public $db;
    public $sessionTableName;

    public function __construct() 
    {
        $this->loadConfig();
        
        session_name($this->sessionName);
        
        session_set_save_handler(
            array($this, "open"),
            array($this, "close"),
            array($this, "read"),
            array($this, "write"),
            array($this, "destroy"),
            array($this, "gc")
        );
    }
    
    public function loadConfig()
    {   
        // application configuration file
        $config_file = dirname(__FILE__) . '/../../../protected/config/main.php';
        $find = array('<?php', '?>');
        $replace = array('', '');
        $configuration = eval(str_replace($find, $replace, file_get_contents($config_file)));
        
        $this->sessionName = $configuration['components']['session']['sessionName'];
        
        $this->db = $configuration['components']['db'];
        $this->sessionTableName = $configuration['components']['session']['sessionTableName'];
    }

    public function open($savePath, $sessionName) {
        return true;
    }

    public function close() {
        return true;
    }

    public function read($id) {
        try {
            $dbh = new PDO($this->db['connectionString'], $this->db['username'], $this->db['password']);
            $statement = $dbh->prepare("SELECT data FROM $this->sessionTableName WHERE id = :id AND expire > :expire");
            $statement->bindParam(':id', $id, PDO::PARAM_STR);
            $statement->bindParam(':expire', time(), PDO::PARAM_INT);
            $statement->setFetchMode(PDO::FETCH_ASSOC);
            $result = $statement->execute();
            if ($result) {
                $row = $statement->fetch();
                return $row['data'];
            }
        } catch (PDOException $e) {
            //
        }

        return '';
    }

    public function write($id, $data) {
        return true;
    }

    public function destroy($id) {
        return true;
    }

    public function gc($maxlifetime) {
        return true;
    }
}

new SessionSaveHandler();

That's It!

Feel free to comment on the approach used and suggest any other tips.

1 0
6 followers
Viewed: 16 523 times
Version: Unknown (update)
Category: How-tos
Written by: Thanasis Fotis
Last updated by: Boaz
Created on: Nov 20, 2012
Last updated: 10 years ago
Update Article

Revisions

View all history

Related Articles