Cookie stored on computer and cookie code in MySQL database does not match

Hi,

I cannot work out why the cookie on my computer does not match the cookie id stored in the MySQL database.

I have this function creating a cookie

function GetCartId()
{
// This function will generate an encrypted string and
// will set it as a cookie using set_cookie. This will
// also be used as the cookieId field in the cart table

if(isset($_COOKIE["cookieid"]))
{
return $_COOKIE["cookieid"];
}
else
{
// There is no cookie set. We will set the cookie
// and return the value of the users session ID

session_start();
setcookie("cookieid", session_id(), time() + ((3600 * 24) * 4));
return session_id();
}
}

And this code is used to enter the cookie ID into MySQL table (add an item to the basket):

@mysql_query("insert into cart(cookieId, itemId, qty) values('" . GetCartId() . "', $itemId, $qty)");

But then another query says this (get data about products added to basket only for user with particular cookie on their computer):

$result = mysql_query("select * from cart inner join items on cart.itemId = items.itemId where cart.cookieId = '" . GetCartId() . "' order by items.itemName asc");

This mysql query does not work. The problem is the “cart.cookieId='” .GetCartId() .“'”. Without this it is fine. With it, it does not work.

As you can see above the cookieId is defined by the GetCartId function. As far as I can tell it must match but with this bit of code it does not work.

Please help,

Matt.

Dear Matt,

I think after set_cookie, please echo $_COOKIE[“cookieid”] to see if value of it match with one insert to DB.

Within the MySQL I had set the cookie id to VarChar(20) so only the first 20 characters/numbers were being stored. My cookie is 32 characters/number on the computer. Is 32 the standard or should I cater for larger values?

Matt.