deleting old cookie

This code should remove the old cookie and set replace with a new one but seems to just create a new cookie of the same name. So I end up with duplicates.

Any idea why?






		    // the name of the cookie


            $siteIDCookieName = Yii::app()->user->id."viewersiteid";


            // remove the old cookie


            unset(Yii::app()->request->cookies[$siteIDCookieName]);


            // create a new cookie


            $cookie = new CHttpCookie($siteIDCookieName,$_POST['SiteInfo']['SiteInfoID']);


            // set expiry time


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


            // assign the new cookie to the cookies collection


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


            // set the siteid var


            $siteID = Yii::app()->request->cookies[$siteIDCookieName]->value;            


            // set the siteid user state


            Yii::app()->user->setState('siteid', $siteID);


any help/thoughts on this please? thanks

When you call unset(), it actually tries to remove the cookie from the collection by sending an expired cookie with the same name. You next call to add the cookie will send a cookie that is not expired. So although you see two cookies with the same name, only one of them is valid. This is expected.