Is it possible to set and access multiple values in cookies in php? I am from an ASP background and to set a cookie with different values I would use something similar to this:
Response.Cookies("Items")("Lines") = intCount
Response.Cookies("Items")("User")=intUser

and then to get the values:
Request.Cookies("Items")("Lines")
Request.Cookies("Items")("User")

So as you can see this sets one cookie but with 2 variables with different values. I know you can set a cookie with php with one value:

setcookie ("cookiename","value");

But is there any way to set it with multiple variables as with asp?
Thanks.

    Yes there is one way i know quite easy but you'll need to make it an array all the time.. There is im sure another way probably easier than mine

    $value ="planetsim|2"; //username and userid 
    setcookie("name",$value,time()+60); //an example
    

    It will look like this when called

    planetsim|2

    To fix it up

    $values = explode("|",$_COOKIE['name']);
    echo $values['0']."<br />".$values['1'];
    

    Will print

    planetsim
    2

    But yes that is a harder way as you must keep exploding the cookie to do that.

      Found an easier way

      sorry for the confusion if any caused

      setcookie("cookie[one]","planetsim",time()+60);
      setcookie("cookie[two]","2",time()+60);
      

      Basically the same values i did before
      but to get them do this

      echo $_COOKIE['cookie']['one']."<br />".$_COOKIE['cookie']['two'];
      

      Take a look hear as well

      http://www.php.net/manual/en/function.setcookie.php

        I prefer the first way you did it. From the looks of it the second method creates multiple cookies, which I would rather not do.
        Thanks for the help.

          6 years later

          I know this is an old thread, but using the following code isn't the best approach (especially if you allow the character | to be entered) and assuming you want your cookie to be read by other sites:

          $value ="planetsim|2"; //username and userid
          setcookie("name",$value,time()+60); //an example 

          Instead perhaps this approach should be taken.

          Reference: here's the parameters that setcookie uses.

          bool setcookie ( string $name [, string $value [, int $expire= 0 [, string $path [, string $domain [, bool $secure= false [, bool $httponly= false ]]]]]] )

          1) Normally I set my cookie variables into a single variable so that I can manage them a little easier in my code.

          $myCookieVars = ''.$variable1.'[]'.$variable2.'';

          Note: I use the characters [] as a delimiter between my variables. This will allow you a more unique identifier when exploding the variables later on and still allows you to use other characters in your variables (especially if you plan on using this for login functions). If you want more than two variables, then you just need to follow the format above.

          2) Next you'll create your cookie. As noted above, there are several input parameters you can use. In general you want to at least use the following.

          setcookie('myCookieName', $myCookieVars, time()+60, '/', '.www.mysite.com')

          There are a couple things to note here.

          First, you need to name your cookie so that it's unique to your site.

          Second, the time function is used to tell the users system when a cookie should expire. Depending on your use you may want to play around with the time settings.

          Third, the / indicates the string path. In this example we're telling the cookie that it can be used in all paths of the domain. If you want to restrict it to a certain directory (and sub directories), then you'd use something similar to /foo/.

          Fourth, this is probably the most important. This sets the cookie for your website. If you don't use it, then you're allowing other websites to read your cookie information. In this example, we're saying use this on the subdomain www. To specifiy a subdomain you'd simply use .subdomain.mysite.com. The . in front of the www.mysite.com isn't required but it should be used so that it's compatible with more browsers.

          Depending on how secure you want to make the cookie and if you use SSL then you can add additional parameters.

          3) Now to read the cookie in, you'll want to do it similar to planetsim's example, but you can skip a step.

          list($var1, $var2) = explode('[]', $_COOKIE['myCoookieName']);

          What this will do is instead of having to call your variables thorugh an array such as $values['0'] and $values['1'], you can assign the variables a unique name in the order you want them.

          So lets say your cookie contains a name and birthdate and you want to be able to use it on your site. Here's a quick and dirty example with the entire code.

          $myCookieVars = ''.$name.'[]'.$birthday.'';
          
          setcookie('myCookieName', $myCookieVars, time()+60, '/', '.www.mysite.com')
          
          list($name, $birthday) = explode('[]', $_COOKIE['myCoookieName']);
          
          echo 'Welcome Back '.$name.'<br />';
          echo 'Birthdate: '.$birthday;

          This way it's a little easier to keep track of what you have coming in and out of the cookie. Hope this helps other members looking for something similar.

            Write a Reply...