Jump to content


Photo

Change cookie value on the fly


  • Please log in to reply
1 reply to this topic

#1 Raider2044

Raider2044
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 11 March 2006 - 01:42 PM

Hi, searched the forums, but to no avail in rectifying my distinct problem, so here goes.

I have recently started using cookies which are being set via PHP's [!--coloro:#993399--][span style=\"color:#993399\"][!--/coloro--]setcookie()[!--colorc--][/span][!--/colorc--] function.
I have successfully been able to write cookies, read from them; and even delete them.

What I am wanting to do is be able to edit a cookie on the fly. Or if need be, delete the original cookie, and set a new cookie simultaneously.

I created a little set-up in order to work this out, but haven't been able to as of yet, so I desperately am trying out the forums. :P

I have a page with the colors [!--coloro:#009900--][span style=\"color:#009900\"][!--/coloro--]green[!--colorc--][/span][!--/colorc--], [!--coloro:#009900--][span style=\"color:#009900\"][!--/coloro--]blue[!--colorc--][/span][!--/colorc--] and [!--coloro:#009900--][span style=\"color:#009900\"][!--/coloro--]red[!--colorc--][/span][!--/colorc--].
I want to set the variable [!--coloro:#3333FF--][span style=\"color:#3333FF\"][!--/coloro--]$style[!--colorc--][/span][!--/colorc--]. If no value is set, it is defaulted to green. If a value is indeed set (read from the cookie), then it will simply use the set value for [!--coloro:#3333FF--][span style=\"color:#3333FF\"][!--/coloro--]$style[!--colorc--][/span][!--/colorc--].

Everything seems to work when the first cookie is set, and the page is revisited, but I am having difficulties in being able to change my choice to another color once the cookie has been set. This is my dilemma.

***index.php***
<?php
if (isset($_COOKIE["style"])) {
    $style = $_COOKIE["style"];
 }
else {
    $style = "green";
    }

echo "<body>";
echo "<h1>you have chosen... ".$style."</h1>";
?>
                <a href="style.php?style=green">Green (default)</a><br>
                <a href="style.php?style=blue">Blue</a><br>
                <a href="style.php?style=red">Red</a><br>

</body>

When a link is clicked, it sets a value for [!--coloro:#3333FF--][span style=\"color:#3333FF\"][!--/coloro--]$style[!--colorc--][/span][!--/colorc--] via [!--coloro:#FF6600--][span style=\"color:#FF6600\"][!--/coloro--]style.php[!--colorc--][/span][!--/colorc--].
As mentioned, it all works good the first time.
All I want is a way that will succesfully change the cookie when a different link is sent.
NOTE: I've only set the cookies to last for 1 minute, but I eventually plan on using them for much longer periods of time, but I figure that it won't have an impact on the design.

The following is just useless code that I have been using, but hopefully it will give you an idea of what I've been doing so far. I commented out one line there, it is the closest I have come to my goal. But the code doesn't work all that well at all. It seems that [!--coloro:#993399--][span style=\"color:#993399\"][!--/coloro--]setcookie()[!--colorc--][/span][!--/colorc--] cannot be used more than once. ???

***style.php***
<?php
if (isset($_COOKIE["style"])) {
    setcookie("style", "", time()-60, "/");
    //setcookie("style", $style, time()+60, "/");
    }
else {
    setcookie("style", $style, time()+60, "/");
    }
header ("Location: index.php");

?>

Thanks again, all your helpful comments will be most appreciated.

Raider2044.

#2 Raider2044

Raider2044
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 15 March 2006 - 12:10 PM

Updating, I had a few moments the other day to work on a solution and have still yet to find one that works suitably.
So I thought it might be best to simplify the question.

Is there any way to change the value of a cookie once already set? That's pretty much it...

I am unable to do so by simply resending a value via [!--coloro:#993399--][span style=\"color:#993399\"][!--/coloro--]setcookie()[!--colorc--][/span][!--/colorc--], as the original cookie is still in place.
I am also unable to decipher a working way in order to delete the original and then set a new one, in the one process.

I will make note that I can infact delete a cookie, but am not able to set a new one without having to resubmit it entirely seperately.

So, hopefully I get an answer of any sorts, if not, thanks for reading my question over in the least.

:P




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users