Jump to content

Change cookie value on the fly


Raider2044

Recommended Posts

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***
[code]
<?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>
[/code]

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

?>
[/code]

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

Raider2044.
Link to comment
Share on other sites

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.

[b]Is there any way to change the value of a cookie once already set?[/b] 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
Link to comment
Share on other sites

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.