Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Is it possible to change the value of a cookie? for example, say i create a cookie named 'user' with the value 'john', but then later in the program, i wanted to change the cookie value of user from 'john' to 'johhny', how would I do this? Id appreciate any help. Thanks.

Replies are listed 'Best First'.
Re: Re-Setting a Cookie
by dvergin (Monsignor) on Aug 07, 2001 at 20:03 UTC
    There is an ambiguity in your question. When you say "later in the program, i wanted to change the cookie value...", it is not clear whether you mean during the same page load or during a later page load.

    If you are trying to change the cookie during the same page load, you can do that by changing what you plan to send in the HTTP header. But once the header has been sent, you cannot backtrack during the processing of that page and change what was sent in the header.

    On the other hand, if you want to change the value of the cookie while processing a later page request, the advice given in the other responses is correct. Just write right over the previous cookie with the identical path and cookie name but with a new value.

    Note also, when you say, "It is setting the new value, but its pulling back the old one when I display it to the screen" it would help us to help you if you told us:

    • why you think it is setting the new value (how did you confirm that),
    • how you are grabbing the value for re-display.

    The devil is in the details and if you are uncertain about what is going wrong, distrust your narrative description of what is happening and show us the code and/or the output.

    HTH, david

Re: Re-Setting a Cookie
by c-era (Curate) on Aug 07, 2001 at 18:55 UTC
    You just set the cookie like you were setting a new one. Just make sure that the path matches, and the old cookie should be overwritten.
      Ive tried this, but it seems to retain the old value :(
        You need to make sure that everything except the value and/or exiration time is the same. I just tried it using IIS with IE and Netscape and it works for me.
Re: Re-Setting a Cookie
by thatguy (Parson) on Aug 07, 2001 at 19:02 UTC
      This is really weird

      It is setting the new value, but its pulling back the old one when I display it to the screen??