n099y
n099y

Reputation: 414

Yii2 Session persistance when redirecting from one action to another

Using Yii2 framework:

The code below creates an endless loop.

Can anyone please explain how I make the session data persist on redirect ?

I have checked and there is not data being transferred, but the session data is set inside searchuser correctly.

public function actionSearchUser()
{
    $session = \Yii::$app->session;
    $session->open();

    $session->set('admin.currentuser.id', "This worked out ok");

    return $this->redirect(['site/modify-user']);
}

public function actionModifyUser()
{

    $session = \Yii::$app->session;
    $session->open();

    if( !($session->has('admin.currentuser.id')) ) 
    {
          return $this->redirect(['site/search-user']);
    }
    else return $this->render('modifyUser');
}

And here is where I setup my session:

    'session'=>array(
        'class' => 'yii\web\Session',
        'name' => 'SESSIONNAME',
        'timeout' => 86400,
        'savePath' => '/path/to/sessions',
        'useCookies' => true,
        'cookieParams' => array(
            'lifetime' => 86400,
            'path' => '/',
            'domain' => 'localhost', 
        ),
    ),

Upvotes: 1

Views: 1243

Answers (1)

n099y
n099y

Reputation: 414

My problem was the domain (I know, I'm stupid).

I have a custom domain (n099y.local) so I needed to change the cookie domain from localhost to n099y.local and everything was fine.

It was showing all the correct session data on the page until I went to another page when the data was again missing because the cookie domain did not match the domain I was on.

Upvotes: 1

Related Questions