Yii Framework Forum: Trouble to get user id as session id in yii2 - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Trouble to get user id as session id in yii2

#1 User is offline   callmesid 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 12
  • Joined: 26-November 16

Posted 03 December 2017 - 03:46 PM

Hi Experts,

I am trying to bind the user is as session id. but getting some error can you help me out???

Situation:

I have a user registration form. On register, it will redirect to another page, where user need to make some selection for there preferences.

I want to get the user registration id as the session id and want to store it to the table associated with preference page.

Once the user preferences is selected, session will terminated and pressing back button will not redirect to the user registration page. If they want to select preference again, then they have to register once again.

Registration form controller code:

{
$model = new UserRegForm();
if ($model->load(Yii::$app->request->post())) {
if ($user = $model->signup()) {
Yii::$app->session->getuser_id;}
{

return $this->redirect(['preference/preferences']);

}

}

return $this->render('UserReg', [
'model' => $model,
]);
}


Any idea?

Thanks in advance,

Regards,

Sid
0

#2 User is offline   umneeq 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 159
  • Joined: 10-October 11
  • Location:Russia

Posted 03 December 2017 - 04:04 PM

You can go two (maybe more) ways.
1. Set some session variable, for example Yii::$app->session->set('settings-are-not-filled', true);
2. Set status for user in db (better, because after logout this settings will work).

In both cases you create, for example, parent FrontendController::init() and check it.
public function init() {
  parent::init();

  if(Yii::$app->request->pathInfo != 'enter/settings') {
    if(Yii::$app->session->get('settings-are-not-filled')) { // or by status
      return $this->redirect('enter/settings');
    }
  }

  return true;
}

0

#3 User is offline   Stefano Mtangoo 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 350
  • Joined: 02-January 12
  • Location:Dar es Salaam, Tanzania

Posted 03 December 2017 - 11:50 PM

View Postcallmesid, on 03 December 2017 - 03:46 PM, said:

Hi Experts,

I am trying to bind the user is as session id. but getting some error can you help me out???

Situation:

I have a user registration form. On register, it will redirect to another page, where user need to make some selection for there preferences.

I want to get the user registration id as the session id and want to store it to the table associated with preference page.

Why not just pass it as a GET parameter in the redirect? something like
//do the registration here
//.....
//....
return $this->redirect(['/preferences/user', 'id'=>$YOUR-ID-HERE]);


In case you want to register ID once, check the passed ID and if exists then redirect user to registration page
And God said, Let there be science...and it was so! Jukwaa la wanaoongea Kiswahili Hapa, and its github repo here
0

#4 User is offline   callmesid 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 12
  • Joined: 26-November 16

Posted 04 December 2017 - 02:57 AM

Hi experts,

Thanks for your reply.

Can I start session in "UserReg" and end it in "preference" page with the session id as the user Id. Though this id is not the constant for the same user.
If a user register with same credential, that user will get new id. In that case session Id will be the new registration id of that user.

Regards

Sid
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users