Jump to content

Is this shopping cart going to perform under stress?


tommyda

Recommended Posts

Hi I am developing a shopping cart for a client but im worried that the way i have put it together is going to cause problems in the future.

 

At the beginning of each page i have a small script to check if the cart cookie exists if not create a new one. If a cookie exists check whether this cart has been paid, if it has create a fresh five digit random cookie.

 

<?php
session_start();

//Check if session user id is set and if so check it against the database

if (isset($_SESSION['user_id']))
{
$check = mysql_query("SELECT * FROM users WHERE id = '$_SESSION[user_id]'")or die(mysql_error());

//if there are problems unset the session
if(!mysql_num_rows($check)=='1')
{
session_unset();
session_destroy();};
};

//NEW CART COOKIE FUNCTIONS

function createcart()
{
srand ((double) microtime( )*10000000000);
$random_number = rand(1000, 99999 );
$new_id = $random_number;
setcookie("cart", $new_id, time()+2592000);
}



//CHECK FOR CART COOKIE
if (!isset($_COOKIE['cart'])){

//NO COOKIE, CREATE ONE
createcart();

}
else
//COOKIE FOUND CHECK ORDERS
{
$query = mysql_query("SELECT payment_status FROM orders WHERE cart_id = '$_COOKIE[cart]'")or die(mysql_error());
$row = mysql_fetch_array($query);
if ($row['paypal_txn_id']!='unpaid')
{
//COMPLETED ORDER FOUND CREATE NEW COOKIE
createcart();
header("Location: " . $_SERVER['php_SELF']);
};};

?>

 

Now when a user adds a product to their cart it inserts into cart table the random 5 digit cookie and the product id.

 

When the user clicks checkout and registers their details or logs in with their premade account (and sets $_SESSION['user_id']) the script also creates an order with id, cart = COOKIE['cart'], user_id = SESSION['user_id'], payment_status = 'unpaid' , payment type = ''

 

Now we have created the order the user can choose a payment option

 

Cheque or paypal. If they choose paypal they will be forwarded to paypal to complete the payment. Once the payment is complete we recieve an instant payment notification and the order status is updated to 'Paid'

 

which should reset the users cookie and give them a new one.

 

I dont have any code problems and it is working well however I would like to know if this metod is going to perform with 100 + users online at once and if you think this method is good practice.  I noticed a few other ecommerce threads and people have done it differently.

 

I hope it all makes sense.

 

Thanks for reading.

 

Tommy

 

 

 

 

Link to comment
Share on other sites

PHP and MySQL are known to be able to handle a serious amount of users regardless of the written code. Like a sponge it's possible to squeeze a few more drops with sufficient added force. So may a well written script serve more users then a bad written script and a well written script that is also optimized may serve even a few more users.

 

Don't worry too much though in the first year your client is only building his customer base. In the second year however your client has an existing customer base and prospects by which the server load will increase and you may want to start looking at buying an additional server, clustering and optimizing your script for use on multiple servers. In the first step you may even want to put your database on a separate server so your current server only handles PHP.

 

And when buying additional hardware make sure the server on which PHP runs has a high CPU power and medium memory and your database server has lots of memory.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.