how to pass session id in other controller ?

Hi,Can I ask how can i pass the session id in other controller ?..this is what I encounter, I logged in sitcontroller then if it is successfully e redirect it to my admin page,in the admin page i tried to echo

but it is empty.

In sitecontroller the session id is not empty it has value.

how can i get the session id to other controller ?

Thank you in advance.

Sessions should carry through redirects unless the user has cookies disabled. Your problem isn’t essentially anything to do with controllers, but incorrectly saving your session state? You’ll have to share your code to see what’s going on.

Sitcontroller




<?php


namespace app\controllers;


use app\models\RegisterForm;

use app\models\User;

use Yii;

use yii\filters\AccessControl;

use yii\web\Controller;

use yii\filters\VerbFilter;

use app\models\LoginForm;

use app\models\ContactForm;


class SiteController extends Controller

{

    public function behaviors()

    {

        return [

            'access' => [

                'class' => AccessControl::className(),

                'only' => ['logout'],

                'rules' => [

                    [

                        'actions' => ['logout'],

                        'allow' => true,

                        'roles' => ['@'],

                    ],

                ],

            ],

            'verbs' => [

                'class' => VerbFilter::className(),

                'actions' => [

                    'logout' => ['post'],

                ],

            ],

        ];

    }


    public function actions()

    {

        return [

            'error' => [

                'class' => 'yii\web\ErrorAction',

            ],

            'captcha' => [

                'class' => 'yii\captcha\CaptchaAction',

                'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,

            ],

        ];

    }




    public function actionRegister()

    {


        $model = new RegisterForm();







       if ($model->load(Yii::$app->request->post())) {

            if ($model->validate()) {

                // form inputs are valid, do something here

                return;

            }

        }


        return $this->renderPartial('register', [

            'model' => $model,

        ]);

    }





    public function actionIndex()

    {


        $model = new LoginForm();

        $user = new User();


        if ($model->load(Yii::$app->request->post()) && $model->login()) {

       

             $role = $user->getRole($model->username);


         


             if($role->role == 'A'){

                  return $this->redirect(array('adminsite/index'));

             }elseif ($role->role == 'U'){

                  return $this->render('about');

             }








        } else {


            return $this->renderPartial('index', [

                'model' => $model,

            ]);

        }

    }


    public function actionLogin()

    {

        if (!\Yii::$app->user->isGuest) {

            return $this->goHome();

        }




        $model = new LoginForm();




        if ($model->load(Yii::$app->request->post()) && $model->login()) {

           

            return $this->goBack();

        } else {


            return $this->render('login', [

                'model' => $model,


            ]);

        }

    }


    public function actionLogout()

    {

        Yii::$app->user->logout();


        return $this->goHome();

    }


    public function actionContact()

    {

        $model = new ContactForm();

        if ($model->load(Yii::$app->request->post()) && $model->contact(Yii::$app->params['adminEmail'])) {

            Yii::$app->session->setFlash('contactFormSubmitted');


            return $this->refresh();

        } else {

            return $this->render('contact', [

                'model' => $model,

            ]);

        }

    }


    public function actionAbout()

    {

        return $this->render('about');

    }

}




Adminsitecontroller




<?php




namespace app\controllers;


namespace app\controllers;




use app\models\User;

use Yii;

use yii\filters\AccessControl;


use yii\filters\VerbFilter;







class AdminsiteController extends \yii\web\Controller

{


    public $layout = 'adminmain';




    public function behaviors()

    {

        return [

            'access' => [

                'class' => AccessControl::className(),

                'only' => ['logout'],

                'rules' => [

                    [

                        'actions' => ['logout'],

                        'allow' => true,

                        'roles' => ['@'],

                    ],

                ],

            ],

            'verbs' => [

                'class' => VerbFilter::className(),

                'actions' => [

                    'logout' => ['post'],

                ],

            ],

        ];

    }




    public function actionIndex()

    {

       


        if(isset(Yii::$app->session->id)){

           return $this->render('index');

       }


        return $this->redirect(array('site/index'));


    }


    public function actionTest(){

        return $this->render('test');

    }


    public function actionLogout()

    {

        Yii::$app->user->logout();


        return $this->goHome();

    }


}






adminsite/index.php




<?php

/* @var $this yii\web\View */

$this->title = 'Adminss';

$this->params['breadcrumbs'][] = $this->title;








   echo $this->render('admin')




?>






adminsite/admin.php





<?php




echo "session".Yii::$app->session->id;

   $message = "hello";


   echo $message;

?>






In my Layouts/amdinmain.php

I have this code




 echo Yii::$app->user->identity->username;



After log-out If I pressed the backbutton,

I have this error




 PHP Notice – yii\base\ErrorException

Trying to get property of non-object


 in C:\wamp\www\yiibasic\views\layouts\adminmain.php at line 239




        </li>

    </ul>

    <!-- /.dropdown-alerts -->

</li>

<!-- /.dropdown -->

<li class="dropdown">

    <a class="dropdown-toggle" data-toggle="dropdown" href="#">

        <i class="fa fa-user fa-fw"></i> <?php

 

        echo Yii::$app->user->identity->username;?> <i class="fa fa-caret-down"></i>

    </a>

    <ul class="dropdown-menu dropdown-user">

        <li><a href="#"><i class="fa fa-user fa-fw"></i> User Profile</a>

        </li>

        <li><a href="#"><i class="fa fa-gear fa-fw"></i> Settings</a>

        </li>

        <li class="divider"></li>

        <li> <i class="fa fa-sign-out fa-fw"></i><?= Html::a(Html::encode('Logouts'),$url = ['logout'],$options = ['data-method' => 'post']) ?>

        </li>









<?php

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

echo "session".Yii::$app->session->id;

   $message = "hello";


   echo $message;

?>




Yii::$app->session->id is sessin_id,it’s don’t use login verification, because the session_id is same for each session

The error is in my Layout/adminmain.php

It’s pointing in this line




 PHP Notice – yii\base\ErrorException

Trying to get property of non-object


 in C:\wamp\www\yiibasic\views\layouts\adminmain.php at line 239




        </li>

    </ul>

    <!-- /.dropdown-alerts -->

</li>

<!-- /.dropdown -->

<li class="dropdown">

    <a class="dropdown-toggle" data-toggle="dropdown" href="#">

        <i class="fa fa-user fa-fw"></i> <?php

 

        echo Yii::$app->user->identity->username;?> <i class="fa fa-caret-down"></i>

    </a>

    <ul class="dropdown-menu dropdown-user">

        <li><a href="#"><i class="fa fa-user fa-fw"></i> User Profile</a>

        </li>

        <li><a href="#"><i class="fa fa-gear fa-fw"></i> Settings</a>

        </li>

        <li class="divider"></li>

        <li> <i class="fa fa-sign-out fa-fw"></i><?= Html::a(Html::encode('Logouts'),$url = ['logout'],$options = ['data-method' => 'post']) ?>

        </li>






the session is not login, so Yii::$app->user->identity->username is nonentity, it like this:


echo Yii::$app->user->isGuest ? 'guest' : Yii::$app->user->identity->username;

I don’t want to echo guest I want to go back to my Login how to do that ?

Thank you in advance.

Thank you I fixed it.