p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

beginning_php thread: shopping cart and cookie problem


Message #1 by "john" <dijiangg@h...> on Sat, 8 Feb 2003 06:37:42
Hi, I have a problem when I code my shopping cart using PHP, I use cookies 
to store the uerid (use session_id()), like this:

if(isset($_COOKIE["cartId"]))
return $_COOKIE["cartId"];
else	
{session_start();
setcookie("cartId", session_id(),time()+((3600*24)*30));
return session_id();
}

the problem is that in the show shopping items function (page), after I 
added one item,if I click the refresh button on the browser, then the 
newly added item will be added again. i tried several ways to solve 
thisproblem, but I don't get a solution. I think I should not delete the 
userid at this moment, since the user has not check out, if the uerid is 
deleted, then the shopping can't be finished, what should I do?

any suggestion?

thanks in advance.

john
Message #2 by "Nikolai Devereaux" <yomama@u...> on Mon, 10 Feb 2003 19:46:09 -0800
> the problem is that in the show shopping items function (page), after I
> added one item,if I click the refresh button on the browser, then the
> newly added item will be added again. i tried several ways to solve
> thisproblem, but I don't get a solution. I think I should not delete the
> userid at this moment, since the user has not check out, if the uerid is
> deleted, then the shopping can't be finished, what should I do?


Well, refreshing a page will cause all form data to be resubmitted.  What you
do in that situation is up to you -- you can keep track of the last page
visited, and the form inputs to that page.  If the user is submitting the same
data to the same page twice, you can ignore it.

Another option is to perform modifications to a user's shopping cart in an
intermediary page, then redirect the user to the "show cart" page.

If the user refreshes their browser, they're only refreshing the "show cart"
page, and not the "modify cart" page.


hth,

Nik


  Return to Index