Arfeen
Arfeen

Reputation: 2623

Yii Session: Adding more info in "yiisession"

Is there any possibility in Yii to add more info in "yiisession" table.

Be default the table has only "id", "expire" and "data" columns, I want to add another for a "userid" for my own use.

The reason to add the column so that my super user can forcefully logout any user. Since the "id" written in Yii session is encrypted.

Any idea how to do that ?

(If it is not possible will it be ok to parse the "data" column to get desired session value ?)

Upvotes: 2

Views: 2397

Answers (1)

cebe
cebe

Reputation: 3819

You can create your own session storage class that can do the things you need by extending CDbHttpSession and adding the methods and properties you need. For example adding a id column could look like this:

class MyDbHttpSession extends CDbHttpSession
{
    public function setUserId($userId)
    {
        $db=$this->getDbConnection();
        $db->setActive(true);
        $db->createCommand()->update(
            $this->sessionTableName,
            array('userId'=>$userId), // I asume you added a column 'userId' to your session table
            'id=:id',
            array(':id'=>session_id())
       );
    }
}

Place your class in your application components directory and add it to your application config by setting the session component class to your new implementation:

// ...
'components' => array(
    // ...
    'session' => array(
        'class' => 'application.components.MyDbHttpSession',
        // ...
    )
    // ...
)

Upvotes: 3

Related Questions