Hi,
I have got a working shopping cart script that uses a cookie that is created the first time something is added to the cart. However, when something is added to the cart for the first time a cookie is created, but nothing is displayed on the page (it should display the cart) unless the page is refreshed. This is only a problem with the first item - when subsequent items are added the cart page appears because the cookie has already been created.
So I guess the problem is that something in the cookie creation process stops the script from progressing any further. Here's the section of the code that checks for the existence the cookie and creates it if it doesn't exist:
function GetCartId()
{
if(isset($_COOKIE["cartId"]))
{
return $_COOKIE["cartId"];
}
else
{
function makeCookieId()
{
$salt = "abcdefghojklmnopqrstuvwxyz0123456789";
srand((double)microtime()*1000000);
$i = 0;
while ($i <= 20)
{
$num = rand() % 35;
$tmp = substr($salt, $num, 1);
$pass = $pass . $tmp;
$i++;
}
return $pass;
}
$id = "DT".makeCookieId();
session_start();
session_id($id);
setcookie("cartId", session_id(), time() + ((3600 * 24) * 2));
return session_id();
}
}
Is there anything in the else statement that would stop the script from going any further? Would it be possible to get the page to automatically refresh once the cookie has been created?
As usual, your help would be greatly appreciated....🙂