How To Share Session In Frontend And Backend

I have created on project using boilerplate architecture, in this, I have installed an admin module to manage the activities of the site in backend,this admin module is accessible to site admin and registered user to manage their blogs,posts and comments and have installed site module also under frontend to view blogs and posts, I want if user login using frontend then it should be logged in backend also.

When I access


Yii::app()->user->role

in the frontend/site/index/index (1. index is controller and 2.index is action under IndexController) it returns ‘admin’ as output but when I access


Yii::app()->user->role

under backend/admin/index/index

it returns NULL.

How can I share user session in frontend and backend both?

You need to change your session storage from PHP’s default to one that can be shared between domains/subdomains, for example CDbHttpSession.

Hi Wiseion3,

As per your suggestion I added following code in both backend/config/main.php and frontend/config/main.php :


'session' => array (

    'class' => 'system.web.CDbHttpSession',

    'connectionID' => 'db',

    'sessionTableName' => 'Session',

),

and created table like this :

CREATE TABLE IF NOT EXISTS Session (

id char(32) COLLATE utf8_unicode_ci NOT NULL,

expire int(11) DEFAULT NULL,

data blob,

PRIMARY KEY (id)

)

But still no luck, could you please elaborate your solution?

Thanks,

@ Dinesh Please try some thing like this :




 'session' => array(

          'sessionName' => 'session',

          'class' => 'CDbHttpSession',

          'autoCreateSessionTable'=> false,

          'connectionID' => 'db',          

          'sessionTableName' => 'Session',

          'timeout' => 300,          

        ),

In user component u need to add below attribute.


'stateKeyPrefix' => 'a prefix for the name of the session variables storing user session data.',

It is working for me. :)

Hello Guys,

anyone cann show me the full example for this on github or bitbucket please. I never used Session before Thx for your Help