Authentication if not user and change menu after log in

Im new in yii , this is my question:

  1. How to set up session for detecting if user is not log in .

  2. After user log in show and hide div.

Menu bar / Navigation Bar :

 <div class="rightPane">

<div class="menuWrapper">

&lt;div class=&quot;menuItem&quot;&gt;


    &lt;a href=&quot;#&quot;&gt;&lt;?php echo Yii::t('labels', 'HOME'); ?&gt;&lt;/a&gt;


&lt;/div&gt;                        

</div>

<div class="menuWrapper">

&lt;div class=&quot;menuItem&quot;&gt;


    &lt;a &gt;&lt;?php echo Yii::t('labels', 'CONTACT'); ?&gt;&lt;/a&gt;


&lt;/div&gt;                        

</div>

<div class="menuWrapper">

&lt;div class=&quot;menuItem&quot;&gt;


    &lt;a href=&quot;#modal&quot; &gt;&lt;?php echo Yii::t('labels', 'SIGNIN'); ?&gt;&lt;/a&gt;

</div>

</div>

<div class="menuWrapper">

&lt;div class=&quot;menuItem&quot;&gt;


    &lt;a href=&quot;&lt;?php echo Yii::app()-&gt;baseUrl ?&gt;/register&quot;&gt;&lt;?php echo Yii::t('labels', 'REGISTER'); ?&gt;&lt;/a&gt;


&lt;/div&gt;                        

</div>

<div class="menuWrapper">

  &lt;div class=&quot;menuItem&quot;&gt;


      &lt;a href=&quot;#&quot; &gt;&lt;?php echo Yii::t('labels', 'signout'); ?&gt;&lt;/a&gt;


  &lt;/div&gt;                        

</div>

Here I wish after log in "Sign in" and "Register" should be hide , then display a new div for "Sign Out"

Log in code from Controller :

$username = $_POST[‘username’];

$userpass = $_POST[‘userpass’];

$record=Games::model()->findByAttributes(array(‘email’=>$username));

if($record===null){

//somethings

}else if($this->checkPassword($record->password,$userpass)){

//somethings

}else

{

&#036;this-&gt;_id=&#036;record-&gt;id;


&#036;this-&gt;_email=&#036;record-&gt;email;





Yii::app()-&gt;user-&gt;setState('id', &#036;record-&gt;id);


Yii::app()-&gt;user-&gt;setState('email', &#036;record-&gt;email);





//go to somethings

}

You can use build in Yii functionality. Yii::app()->user->isGuest will return true/false if user is logged.

Please check

http://www.yiiframework.com/doc/api/1.1/CWebUser#isGuest-detail

Hi. thanks for your reply …

I have already tried isGuest

<?php if(Yii::app()->user->isGuest): ?>

            	&lt;div class=&quot;menuWrapper&quot;&gt;


                	&lt;div class=&quot;menuItem&quot;&gt;


                    	&lt;a href=&quot;#modal&quot; &gt;&lt;?php echo Yii::t('labels', 'SIGNIN'); ?&gt;&lt;/a&gt;


             	&lt;/div&gt;                  


                &lt;/div&gt;


                &lt;div class=&quot;menuWrapper&quot;&gt;


                	&lt;div class=&quot;menuItem&quot;&gt;


                    	&lt;a href=&quot;&lt;?php echo Yii::app()-&gt;baseUrl ?&gt;/register&quot;&gt;&lt;?php echo Yii::t('labels', 'REGISTER'); ?&gt;&lt;/a&gt;


                    &lt;/div&gt;                        


                &lt;/div&gt;


				&lt;?php else: ?&gt;


                 &lt;div class=&quot;menuWrapper&quot;&gt;


                	&lt;div class=&quot;menuItem&quot;&gt;


                    	&lt;a href=&quot;#&quot; &gt;&lt;?php echo Yii::t('labels', 'signout'); ?&gt;&lt;/a&gt;


                    &lt;/div&gt;                        


                &lt;/div&gt;


                &lt;?php endif; ?&gt;

Yes, "singout" is hide …However, when i logged in … "Sign in" and "Register" are still show and "Sign out" are still hide

Did you know why ?

Hi,

what happens in the background when logging in?

As far as I know, you have to deal with CUserIDentity to authenticate users. You can make up your own authentication logic deriving from this class. (As it is said: "Derived classes should implement authenticate with the actual authentication scheme…")

Give a try to this tutorial. The 1st and 2nd steps are enough to create a simple user authentication method. The key point is in the example at the 1st step, the code goes


$this->_id=$record->id;

this is what makes the difference, because CWebUser::isGuest property would be true for users whose id is set.

Hope it helps,

cheers!