Re: cookies and carts
| From: | Philip Thompson | Date: | Mon, 07 Dec 2009 22:47:43 +0000 |
| Subject: | Re: cookies and carts | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-300293@lists.php.net to get a copy of this message | ||
On Dec 7, 2009, at 4:39 PM, Allen McCabe wrote:
> I have a shopping cart type system set up which keeps track of the cart
> contents using a SESSION variable, where $_SESSION['cart'][$item_id'] is
> equal to the quantity, so the name/value pair is all the information I need.
>
> But sessions are unreliable on the free server I am currently using for this
> website (not my choice), so I had start using cookies because users were
> being sporadically logged out, sometimes just on a page refresh.
>
> I want to find a way to set a cookie to remember the cart items as well, and
> I thought setting a cookie for each item/quantity pair was the way to go
> until I started trying to figure out how to unset all those cookies if the
> user empties their cart.
>
> Is there any way to set cookies with an array for the name? Intead of
> $_COOKIE['item_number'] have $_COOKIE['cart']['item_number'] like
> I have the
> SESSION?
Don't do it this way. At some point (don't know if it's still true), IE had a limit
of 20 cookies per domain - this includes cookie arrays. The proper way to do this would be to hold
some sort of key in a cookie:
user_cart = 'some unique value for this user'
Then, in your PHP code, grab the value of $_COOKIE['user_cart'] to reference data in a
database. Then, you pull the information from the database with this unique key and use it to
display the appropriate items. This is the most secure way to do it (with the proper security
measures ;) and it doesn't put 100's of needless cookies on the user's machine.
Hope this helps.
~Philip