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()



        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



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


<!-- /.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>


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

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


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


        <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