Jump to content

Shopping Cart


ded

Recommended Posts

First time doing a shopping cart.  I figure I have to hold the information within the shopping cart in a database.  How do I link the records to the "session"?  Do I use the session_start();?  Not sure exactly how to do this.

 

The owner of the site does not require the buyer to create an account.

 

 

Regards,

DED

 

Link to comment
https://forums.phpfreaks.com/topic/194967-shopping-cart/
Share on other sites

The way I would do this (and it probably isnt the best way) is:

 

When the user first adds something to the cart, set $_SESSION['mysession'] to a random string based on the time:

$random_hash = md5(date('r', time()));
$_SESSION['mysession'] = $random_hash;

 

This $random_hash would also be inserted into the database holding the shopping cart. The database would probably look similar to:

 

session - the $random_hash

item - the id of the item the user has added to the cart

other stuff here

 

Then the view cart page would run a query to get the ids of all the items linked to the $_SESSION['mysession'] value in the database.

 

You will need to run session_start(); on ALL pages where you use a $_SESSION variable, and this must be called before anything is sent to the browser. Make a habit of making it the first line in each file you need it in.

 

A cronjob to periodically clear any items put in the database more then x time ago (ie 12 hours) would be a good idea to stop the db getting exponentially large

Link to comment
https://forums.phpfreaks.com/topic/194967-shopping-cart/#findComment-1025032
Share on other sites

aleX_hill,

 

That is basically what I did.  Except i created the session id at the time the user first enters the website.  Is this a good idea?

session_start();
if ($_SESSION['sid'] == ""){
$_SESSION['sid'] = rand() . rand() . rand();
}

 

I have everything working perfect.  The add product, delete product.  What I need to do now is give the summary information to paypal.  Basically, even if they order 5 items, just send a total price to paypal.  Once payment is complete, return to my website with the correct session id.  I would then have a order processed screen show all the detailed information and shoot out an email to the buyer.  Where can I get information on how to do this?  Seems everywhere I look on paypal, I would have to create paypal buttons.

 

Any help is much appreciated.

 

Regards,

DED

Link to comment
https://forums.phpfreaks.com/topic/194967-shopping-cart/#findComment-1027839
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.