View Full Version : session-tracking problem

03-27-2004, 10:59 AM
hello friends,

i am stuck in a situation, plz help, the problem is as follows:

i am making a shopping cart, now the home page of the shopping cart shows item categories, and items under it. There is no account login facility yet as there is no requirement. I am able to start a session and track what items are added by the user to their cart and display them all the items, the users are also able to remove the items from their cart.

Now when someone closes the browser without checking out, the session expires and when the same person visits the same site and adds something to his cart, the old items are not retained, which he had chosen earlier, i am not able to figure out, how can i solve this problem i.e. if the user closes the browser and then re-visits the site in the next 1 or 2 days, he should see the items which was added by him earlier. I am storing the session id and all the items added to cart by him in the database.

Please help.
Gaurav Behl

03-27-2004, 11:57 AM
Welcome here !

If you don't want to set up a login (5 minutes work ?) then you need to set a persistent cookie with an expiration of 2 days

I am storing the session id and all the items added to cart by him in the database.

so inside the table with your orders you should have an autonum column. When a new record is generated here, you grab the generated autonum (with mysql_insert_id) and you store that inside the cookie.

So your optimal db-design should be

- orders table --> containing general data about order (userid, starttime, endtime IP etc)
- order-sessions --> containing the orders table PK as foreign key + the session-id. One row for each visit to complete the order
- orderitems tabe --> containing the orders table PK as foreign key + the items PK as foraign key + the ammount

It's when you create a new row inside the order-session table that you set the cookie.
When the user revisits the site, you check for the cookie + run a select against the order-session table. If no record is found, insert a new record inside the order-session table and send the cookie with the same order PK value.
So with each new visit the cookie is renewed and the expirationdate is puched forward.

Once the user comfirm his order, remove the cookie by resending it with a negative expire value.