how to check if there is session

Hi I am confuse in checking the session.I tried this but no luck…After logout in my admin page,If I am going to click the browser back button It will have fatal error,because there is no session anymore…


Trying to get property of non-object




   public function actionIndex()

    {


       if(Yii::$app->session->id !=null){

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

       }


        return $this->redirect(array('site/index')); //back to my login page


    }





   public function actionLogout()

    {

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


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

    }



How can I check the session if it no user logging-in ,I will redirect it to my login page.

Thank you in advance.

You may want to try using isset() to check if an object/array element exists.


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

    // Lorem ipsum

}

still I get fatal error

Would you mind posting the error?

This is the error.





 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; //The error is pointing here

 

        ?> <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>






How to do that after logout,when pressing the back button of browser it will not go back to the previous page.

Thank you in advance.

I fixed it thank you