hii all,

I have a 5 star rating system which writes a cookie as the following when the user rates a product:

$pid = $_GET['pid']; //product ID (primary key)
$ratepostpone = 300; //5 minutes until it expires and the user can rate again
$cookieroot = '.mysite.com'; //my website's directory including sub domains

setcookie('ms-'.$pid,'ms-'.$pid,time()+$ratepospone,'/',$cookieroot);

However I'm finding that when I vote on two products within 5 minutes of each other it is overwritting the cookie. When I check what cookies I have saved on my computer it lists one cookie (ms-483243) and then if I rate another product and re-check my cookies it has deleted ms-483243 and replaced it with (ms-218572)

any ideas why?

Thanks in advance,

Max

I think it has to do with your cookie name:

'ms-'.$pid

This is going to be different for each product that is rated, thus creating another cookie.

Try using one name for the cookie and set the $pid in the cookie value.

$new_value = 'ms-'.$pid;
if ($_COOKIE['RATED_PRODUCTS'] != '') //append product id : is the separator
    $new_value = $_COOKIE['RATED_PRODUCTS'].':'.$new_value;
setcookie('RATED_PRODUCTS',$new_value,time()+$ratepospone,'/',$cookieroot);

so each PID would be seperated by a ':' and to check if the user has already rated it I would have to search for the PID within the value of the cookie?

How long can the cookie value be anyway?

Thanks for the help by the way.

Max

Yes that is correct, you then could do something along the lines of:

if (ereg(':', $_COOKIE['RATED_PRODUCTS']))
{
	// We have more than 1 product id
	$pids = explode(':', $_COOKIE['RATED_PRODUCTS']);
	
	for ($i = 0; $i < count($pids); $i++)
	{
		// Do something with stored product ids
		echo($pids[$i]);
	}
}
else
{
	// We have only 1 product id
	echo($_COOKIE['RATED_PRODUCTS']);
}

I think there is no limit to the length of the cookie value, not 100% sure on this one.

okay thanks. I only need to check to see if a single PID is saved inside the cookie so will this work:

if (eregi($pid, $_COOKIE['RATED_PRODUCTS']))
{
	echo 'you have already rated';
}
else
{
	echo $ratingsystem;
}

To make the PID i'm using rand(0,99999); so it is unlikely that one PID will be found inside another one... but is there a possibility that it might?

Max

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.