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.