Forum Moderators: coopster

Message Too Old, No Replies

PHP Cookie Problem

         

holmags

9:21 pm on May 13, 2006 (gmt 0)

10+ Year Member



Hi I seem to be having a problem with the cookies for my site. Basically they don't seem to be being set at all. Here's my function to set the cookie or call it up:

function GetCartId() {
if(isset($_COOKIE["cartId"])) {
return $_COOKIE["cartId"];
} else {
//generate a cookie id
$cookieID = "";
$possible = "abcdefghijklmnopqrstuvwxyz0123456789";
$n = 0;

// add random characters to $password until $length is reached
while ($n < 15) {

// pick a random character from the possible ones
$char = substr($possible, mt_rand(0, strlen($possible)-1), 1);

// we don't want this character if it's already in the password
if (!strstr($cookieID, $char)) {
$cookieID .= $char;
$n++;
}
}
$path = '/subdomains/cvperfect/httpdocs/';
$hostname = '#*$!';

setcookie("cartId", $cookieID, time() + 3600, $path, $hostname);

return $_COOKIE["cartId"];
}
}

Can anyone see any problems with the code? Would I be better off using a session?

regards

holmags

eelixduppy

11:36 pm on May 13, 2006 (gmt 0)



Just to see if this is the problem, change this:
>>>setcookie("cartId", $cookieID, time() + 3600, $path, $hostname);

to this
setcookie("cartId", $cookieID, time() + 3600);

If this works then you know it has something to do with your path and hostname. I'm not an expert on Cookies [php.net] or anything, but this is what i would try first to isolate the problem.

eelix

holmags

10:12 am on May 14, 2006 (gmt 0)

10+ Year Member



Thanks for replying. I tried leaving the host and path variables out but it stopped working. I changed the path and it seems to be working correctly now.

thanks for your help

holmags