cookies with multple vals

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Alexander Ross

    #1

    cookies with multple vals

    I want a cookie that can have about 5 values. I was told to do something
    like this:

    to set the cookie the first time:
    setcookie("ques tions[]",$_POST['questionid'],time()+60*60*2 4,"/","",0);

    then to add valus to this cookie later:
    setcookie("ques tions[]",$_POST['questionid']);

    the problem is that when I do this, the cookie's first value is just getting
    replaced over and over again.

    How do I handle a cookie so I can just keep pushing values on to it n times
    so that at the end I have an array with n values??

    --
    Alexander Ross
    alexross@bleen. net


  • Gary Petersen

    #2
    Re: cookies with multple vals

    On Fri, 29 Aug 2003 13:51:55 -0500, in message
    <fhN3b.300494$u u5.64849@sccrns c04>, the AI program named "Alexander Ross"
    <alexross@bleen .net> randomly printed:
    [color=blue]
    > I want a cookie that can have about 5 values. I was told to do something
    > like this:
    >
    > to set the cookie the first time:
    > setcookie("ques tions[]",$_POST['questionid'],time()+60*60*2 4,"/","",0);
    >
    > then to add valus to this cookie later:
    > setcookie("ques tions[]",$_POST['questionid']);
    >
    > the problem is that when I do this, the cookie's first value is just
    > getting replaced over and over again.
    >
    > How do I handle a cookie so I can just keep pushing values on to it n
    > times so that at the end I have an array with n values??
    >
    > --
    > Alexander Ross
    > alexross@bleen. net[/color]

    You could store the values in an array. Then you would
    serialize() and base64_encode() it to put it
    into a cookie.

    To get the array back, base64_decode() it and
    unserialize() it.

    Then add whatever items you want to the array
    and do it all over again.

    BTW: It's probably not a good idea to put too much
    junk into the HTTP header; it might reduce efficiency.
    If you have an array with 1000 elements, I advise against
    putting it into a cookie because it would slow things
    down.

    Just use a session variable. Putting an array into a
    session is just too easy.

    Comment

    • MeerKat

      #3
      Re: cookies with multple vals

      Alexander Ross wrote:[color=blue]
      > I want a cookie that can have about 5 values. I was told to do something
      > like this:
      >
      > to set the cookie the first time:
      > setcookie("ques tions[]",$_POST['questionid'],time()+60*60*2 4,"/","",0);
      >
      > then to add valus to this cookie later:
      > setcookie("ques tions[]",$_POST['questionid']);
      >
      > the problem is that when I do this, the cookie's first value is just getting
      > replaced over and over again.[/color]

      What ya need to do is something like this:

      setcookie("ques tions[1]","qid1");
      setcookie("ques tions[2]","qid2");

      you can then get at them with this:

      foreach ($_COOKIE['questions'] as $name => $value) {
      print "$name: $value <br />";
      }

      which will output:

      1: qid1
      2: qid2

      ....I think. :)

      MK.
      [color=blue]
      > How do I handle a cookie so I can just keep pushing values on to it n times
      > so that at the end I have an array with n values??
      >
      > --
      > Alexander Ross
      > alexross@bleen. net
      >
      >[/color]

      --
      MeerKat

      Comment

      Working...