Cookies Don't get Updated

I truly apologize if this is a silly question but, I do this all the time with my own code and I would like to do it Yii style. My application works perfect or I thought so until I double checked the cookies on my browser.

The cookie where I store the user language selection keeps being re-created again and again, like if Yii isn’t capable of reading it. I have checked its path and is correct but…

Here is the code, could you please tell me what I am doing wrong?




protected function setCookie()

	{

		$cookies=Yii::app()->getRequest()->getCookies();

		$cookie=$cookies->itemAt(self::DATA_KEY);

		

		if($cookie && !empty($cookie->value))

		{

			if ($cookie->value == $this->language) return;

			

			$cookie->value = $this->language;

			$cookie->expire=time() + 3600*24*30;

			$cookies->add($cookie->name,$cookie);

		}

		else {

			$cookie = new CHttpCookie(self::DATA_KEY, $this->language);

       		$cookie->expire = time() + 3600*24*30;

       		

       		Yii::app()->getRequest()->getCookies()->add($cookie->name,$cookie);

		}

	}



Could it be that $this->language is empty by default when you create the new cookie? Then the condition in your if statement would fail…

Other than that: Using FF HTTPHeaders extension would show you the “raw” cookie data in the HTTP headers. I often look at them to see what’s really going on under the hood. Maybe it helps tracking down the issue?

Thanks Mike for your hint…

The $this->language was ok but in one scenario. I found my issue when I included a silly conditional statement in the code before the function call in my previous post.

if(!isset($_GET[self::DATA_KEY])) {

$this->language =  Language::model()->getLocalizedLanguage();  

}

elseif($_GET[self::DATA_KEY]!=Language::model()->getLocalizedLanguage() &&

in_array($_GET[self::DATA_KEY],$this->languages)) {


$this->language = $_GET[self::DATA_KEY];

}else // HERE THE SILLY STATEMENT

$this->language = Language::model()->getLocalizedLanguage();

Then is when I realized that one of the languages wasn’t properly set on the URL: my default language ($this->languages[] = Language::model()->primaryLang();), which is set in another function This is why it was confusing at first, I was looking at the wrong place.

Thanks Mike as you point me to the right direction on this issue. I should stop programming when is up to 2AM.