Jump to content

[SOLVED] Cookie duplicates


gevans

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/144775-solved-cookie-duplicates/
Share on other sites

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>';
?>

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.

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 :)

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! ;)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.