Shopping Cart

A

Anonymous

Guest
After some though, I think I've come up with an idea about how to do my shopping cart.

Until one of my "Add To Cart" buttons is pressed, I don't need to do anything. When an "Add To Cart" button is pressed, I will get the user's IP address. With the IP address, I will look for a file name on my server - the file name will be the IP address plus "cart" or something like that.

If the file doesn't exist, create it and add the PartNumber, Description, Quantity, Price and shipping cost. If the file does exist, just add append the new item.

When the View Cart or Checkout button is pressed, I simply go to the script that creates a nice <Table> with all the data and a SubTotal and Total_With_Shipping. The user can then go to the script that sends me their infomation.

After all the shopping is done, another script is run that will delte any .cart files that are older than a certain age - which I will specify in code.

Does this look reasonable? I'm going this way because I'm just getting into PHP. I don't know about cookies and sessions yet.

Do you see any problems with this idea?
 
Don't track users via IP address. Mosts users' IP addresses change frequently. Dial-up users usually get a new IP every time they sign on, and many dial-up and broadband ISPs re-assign IPs mid-session. You cannot depend on a user's IP address to be the same from one minute to the next.

There are two ways to do shopping carts. One is with cookies/sessions alone. Store the user's shopping cart data in the database according to their session ID. This sort of shopping cart is somewhat impermanent. Your cookie has to expire eventually, or the user may purge cookies, or the user may sign on with a different machine or username, in which case their shopping cart data does not travel with them. But it's often quite acceptable.

The other way is to use a registration system, in which the user logs in with a username and password and the shopping cart data is stored according to their username. This way the user has access to a consistent shopping cart from wherever he logs in.

There is no good way, in my experience, to do a shopping cart without a database back-end. The file maintenance alone will kill you if the coding doesn't.
 
Well, if the IP address is going to be changing on me from one minute to the next, I suppose this idea will not work.

But I still don't want to go with the database idea yet. My site only gets about 50-100 visitors per day. And I only get 2 or 3 orders per week. So I think I'd still rather stay with a single shopping cart file for each customer when they decide to "Add To Cart".

How about this....
I write a cookie to the customer's computer with a randomly created file name. Something like a string 10 letters and numbers. The odds of getting two identical file names for two customers online at the same time would be miniscule.

Then, I just use the randomly created file name to store the shopping cart info. So I'm right back where I was. And at checkout time, I can still run a little program to delete old shopping cart files that are left by "tire-kickers" who don't actually finish the order.

The point is, I just don't want to learn MySQL right now. Text files are simple and I think I can keep them from reproducing like rabbits. At most, I would have around 500 shopping cart files and they will all be in one folder that I can easily delete when I decide to go to the database version.

Any other ideas to look out for?
 
DroopyPawn said:
I write a cookie to the customer's computer with a randomly created file name. Something like a string 10 letters and numbers.

Luckly, PHP has a built-in function for this called uniqid().

Then, I just use the randomly created file name to store the shopping cart info. So I'm right back where I was. And at checkout time, I can still run a little program to delete old shopping cart files that are left by "tire-kickers" who don't actually finish the order.

Well, if you're determined not to use a database, then this will work fine. I recommend that you use XML for storing the data, as there are already many libraries available for dealing with XML files, and the format is easily extensible.
 
Back
Top