Page 1 of 1

[SOLVED]Problem deleting a cookie

Posted: Tue Jan 25, 2011 6:15 am
by divedj
After 1 week resaerch without a result somebody here might have a solution for the cookie deleting problem.

I am using a cookie to identify the owner of a shopping cart.

The cookie is set on entry page and passed on to a class for farious cart operations.

The cookie is set with the known code:

Code: Select all

setcookie("name", $value, time()+(60*60*24*14), "/", "www.myDomain.com");
After the user finalized the purchase the cart is empty and the cookie need to be deleted which is done in a class function.
The function itself is called from code running in the background checking for valid pament.

Code: Select all

mysql_query("DELETE FROM ".$dbpraefix."cart WHERE user_id = ".$cart_user."");
setcookie("name", $value, time()-(60*60*24*14), "/", "www.myDomain.com");  // actual code, I also tried to set time value to 1
Creating a test page like the following, setting and deleting the cookie works.

Code: Select all

if(isset($_COOKIE['test']))
    {
        echo "<br /><br />";
        echo $_COOKIE['test'];
        echo "<br /><br />";
    }

   if(isset($_COOKIE['test']) && isset($_POST['delete']))
    {
        setcookie("test", "testcookie", time()-(3600*25), "/");
    }

   if(!isset($_COOKIE['test']) && isset($_POST['set']))
    {
        setcookie("test", "testcookie", time()+(3600*25), "/");
    }

?>

<form action="cookie-test.php" method="post">
    <input type="submit" name="set" id="set" value="Set Cookie" />&nbsp;&nbsp;
    <input type="submit" name="delete" id="delete" value="Delete Cookie" />
</form>
Also setting the path to any different then "/" doesn't work since the cookie has to be avilable in the page directory where it was created, in the class directory where all the processing takes place and in the payment processor where the cart owner need to be identyfied using various methods just to be sure.

If anybody does know a solution for that I would be very glad to hear.

Thanks a lot

Re: Problem deleting a cookie

Posted: Tue Jan 25, 2011 2:21 pm
by AbraCadaver
divedj wrote:After the user finalized the purchase the cart is empty and the cookie need to be deleted which is done in a class function.
The function itself is called from code running in the background checking for valid pament.
Without knowing exactly what is happening I would say that this is the problem. The browser accepts cookies from the server and sends cookies to the server, so the browser must execute the setcookie() code that is supposed to expire the cookie. If a response from a payment processor is executing this code then it won't work.

Maybe have the user redirected to a page and delete the cookie (but this would most likely happen before the payment is verified). So if you must wait for the payment to be verified then you will probably have to store the cart items in a db and then when the payment if verified, that code will delete the items from the db.

Re: Problem deleting a cookie

Posted: Thu Jan 27, 2011 5:03 am
by divedj
Thanks, did not think about the browser output.

The cart contents are stored in a db anyway. The cookie is only used to id the cart owner in case he has to leave the site and want to continue shopping a view hours or maybe a day later.

However, deleteing the cookie works soon as the customer logs in to the download section to pick up his purchase.

Thanks again