Understanding Cookies

I have this in my model when my user logs in successfully:



			$duration=$this->remembered_me ? 3600*24*7 : 0; // 7 days


                        User::model()->updateByPk($this->_identity->id, array(

                         'visit_time' => new CDbExpression('NOW()')



			return true;


Say the user chose "Remember Me" when they logged in, upon returning how do I redirect them to the appropriate page being that they should be already logged in by the cookie…what code do I use and where to I put the code?

Do I put a cookie check somewhere in the controller that loads the homepage? How do I know what the cookie name is?

You’re talking about actionLogin, right? so the user who is already logged in should be redirected to somewhere instead of looking at the login form again.

This can be done simply by checking Yii::app()->user->isGuest, for example:

function actionLogin()


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

        // user is already logged in





Yes this is what I’m talking about…so when I have a Remember Me cookie on the user browser that haven’t expired I can just check if guest and it recognizes the cookie? I should put that in my homepage action that has the login form right? Do I ever need to check the cookie? How would I know the name of the cookie stored in the fashion in my code above?

Wow, that was a lot of questions.

See here:




Usually not.

Usually you don’t have to, but if you want - you can use browser debug panel to see what is actually stored on client-side.

Thanks ORey!

Sorry bout the stream of questions…I was responding on my cell phone in kind of a hurry. I appreciate your response.

Got one more question though. When I do


it redirects me but I get an invalid url because for some reason it is leaving index.php out the url. Do I have to use Yii::app()->request->baseUrl within here?

Got it! It should be like this: