Cookies and how to set them

Currently I'm struggling a bit with setting cookies. I've tried several ways, but I'm stuck at the moment. Perhaps someone can shed some light here…

I tried the following:

$cookies['lang'] = new CHttpCookie('lang', $_GET['lang']);

After that (among other less likely ways):

Yii::app()->request->cookies['lang'] = new CHttpCookie('lang', $_GET['lang']);


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

$cookie = new CHttpCookie('lang', $_GET['lang']);

$cookies['lang']->add('lang', $cookie)

But no cookie…

Trying to figure out why, So looking at CCookieCollection->add() I put an additional throw in.




	throw new CException(Yii::t('yii','CCookieCollection not initialized.'));

And yes I get that exception. So it seems its not initialized. Not sure where to look anymore…

// to send a cookie:

Yii::app()->request->cookies['lang']=new CHttpCookie('lang',$_GET['lang']);

// to read a cookie:


Thanks, finally found where I went wrong, was giving expire a relative timestamp, so cookie was deleted automatically by the browser… :-X

	$cookie = new CHttpCookie($name, $value);

	$cookie->expire = time()+60*60*24*180; 

	Yii::app()->request->cookies[$name] = $cookie;

As conclusion to this discussion, I wrote a Wiki article about cookie management, where problems, like cookie expiration time are discussed.

BTW: Sorry, for reopening over two years old thread.