Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.