gevans Posted February 11, 2009 Share Posted February 11, 2009 Hey guys I'm using a class method to set a cookie to rememebr the last page a user visited.. public function setNewCookie($t,$c) { self::destroyCookie(); $this->cookieContent = $c; setcookie($this->cookieName,$this->cookieContent,"$t", "/");#print($this->cookieName.' '.$this->cookieContent.' '."$t".' '."/"); } $t and $c are time and content respectively and are fine. destroyCookie just puts it in the past before re-creating it public function destroyCookie() { if(isset($_COOKIE[$this->cookieName])) { setcookie("{$this->cookieName}", '', time()-60*60*24*30, "/"); } } Everypage I visit is currently making a brand new cookie of the same name without deleting the old, so my cookie list has 9 cookies of the same name (after nine page visits). Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/144775-solved-cookie-duplicates/ Share on other sites More sharing options...
rhodesa Posted February 11, 2009 Share Posted February 11, 2009 um...one thing I notice is this: self::destroyCookie(); should be: $this->destroyCookie(); Quote Link to comment https://forums.phpfreaks.com/topic/144775-solved-cookie-duplicates/#findComment-759674 Share on other sites More sharing options...
gevans Posted February 11, 2009 Author Share Posted February 11, 2009 After reading this CLICKY it will still work with that, but you're right it is wrong so I've changed it. Still the same problem Quote Link to comment https://forums.phpfreaks.com/topic/144775-solved-cookie-duplicates/#findComment-759682 Share on other sites More sharing options...
rhodesa Posted February 11, 2009 Share Posted February 11, 2009 After reading this CLICKY it will still work with that, but you're right it is wrong so I've changed it. Still the same problem hum...the page describes accessing static content, which you aren't doing. it does indeed work though, which bothers me...i feel it should throw an error... back to your problem though...can you provide more detail on how to duplicate your error...i used this code and i don't get duplicates when i refresh: <?php class cookie { public function __construct ( $n ) { $this->cookieName = $n; } public function setNewCookie($t,$c) { $this->destroyCookie(); $this->cookieContent = $c; setcookie($this->cookieName,$this->cookieContent,$t, "/");#print($this->cookieName.' '.$this->cookieContent.' '."$t".' '."/"); } public function destroyCookie() { if(isset($_COOKIE[$this->cookieName])) { setcookie($this->cookieName, '', time()-60*60*24*30, "/"); } } } $c = new cookie('foobar'); $c->setNewCookie(time()+60,'my value'); print '<pre>'.print_r($_COOKIE,1).'</pre>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/144775-solved-cookie-duplicates/#findComment-759692 Share on other sites More sharing options...
gevans Posted February 11, 2009 Author Share Posted February 11, 2009 OK... I've just changed this; setcookie("{$this->cookieName}", '', time()-60*60*24*30, "/"); to this; setcookie("{$this->cookieName}", "", time()-60*60*24*30, "/"); Note the singe/double quotes in the string $value section. I have no idea why but this seems to have sorted the problem. Quote Link to comment https://forums.phpfreaks.com/topic/144775-solved-cookie-duplicates/#findComment-759698 Share on other sites More sharing options...
rhodesa Posted February 11, 2009 Share Posted February 11, 2009 strange.... also...can you also change this: setcookie("{$this->cookieName}", "", time()-60*60*24*30, "/"); to this please: setcookie($this->cookieName, "", time()-60*60*24*30, "/"); it's one of my pet peeves...when people put a variable inside double quotes without any other text in there...ignore me if you want, but i will sleep better at night if you do Quote Link to comment https://forums.phpfreaks.com/topic/144775-solved-cookie-duplicates/#findComment-759701 Share on other sites More sharing options...
gevans Posted February 11, 2009 Author Share Posted February 11, 2009 strange.... also...can you also change this: setcookie("{$this->cookieName}", "", time()-60*60*24*30, "/"); to this please: setcookie($this->cookieName, "", time()-60*60*24*30, "/"); it's one of my pet peeves...when people put a variable inside double quotes without any other text in there...ignore me if you want, but i will sleep better at night if you do Just because I'm concerned about your health after hearing on the radio earlier that many people in the world may be endangering their helth due to sleep deprevation it's done! Quote Link to comment https://forums.phpfreaks.com/topic/144775-solved-cookie-duplicates/#findComment-759705 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.