Hi All,

A little help is required.

www.neph-aromatics.co.uk

I've created a site and cart using the wfcart system.

However, if you leave the cart for a minute and browse the items, your cart may not contacin the items you have added, or any items at all.

A little help would be great, as I've got little hair left.

Cheers in advance.

🙂

    Can no one help me?

    There must be some suggestions...

    (less and less hair every day)

    🙁

      how can anybody help you?

      you have posted no code, and near zero details of your problem.

      you have basically said "i have a problem"

      and are expecting everyone to guess what it is.

      try and put some effort into your question.

        Handbags at dawn.

        🙂

        I think I was in super stress mode or something.

        Well, let's see. More details.

        During the testing phase the web site works fine. The cart does what it says on the tin.

        The final Production site is rather unstable. The rest of the site, pulling stuff from the database and displaying it, is great, it's just the cart that's unstable. When you add something to the cart, it adds ok. If you go looking for another item, the first item might be there when you return to the cart, and maybe not.

        I have session_start() at the top of each page, each page being a .php page (there are no .html pages). I also call the cart functions before the session starts.

        Here's some code:

        
        // Add cart tools
        include("r/wfcart.php");
        // Start the session 
        session_start();
        session_regenerate_id();
        // Initalise the cart
        $cart =& $_SESSION['cart'];
        if(!is_object($cart)) $cart = new wfCart();
        
        

        Running phpinfo() on each server gives the same results. There are a couple of differences, but these seem to be unconnected to the sessions, timeouts or anything like that.

        I have tried to regenerate the php session id, see code, and this doesn't really do too much. I've also upped the session.gc_maxlifetime time to 3000, in a vane attempt to crack it.

        Here's wfcart:

        <?php
        /*
        ######################################################################
        # __      __          __         ___                                 #
        #/\ \  __/\ \        /\ \      /'___\                                #
        #\ \ \/\ \ \ \     __\ \ \____/\ \__/  ___   _ __   ___     __       #
        # \ \ \ \ \ \ \  /'__`\ \ '__`\ \ ,__\/ __`\/\`'__\/'___\ /'__`\     #
        #  \ \ \_/ \_\ \/\  __/\ \ \L\ \ \ \_/\ \L\ \ \ \//\ \__//\  __/     #
        #   \ `\___x___/\ \____\\ \_,__/\ \_\\ \____/\ \_\\ \____\ \____\    #
        #    '\/__//__/  \/____/ \/___/  \/_/ \/___/  \/_/ \/____/\/____/    #
        #                                                                    #
        #     )   ___                                                        #
        #    (__/_____)                      webforce cart v.1.2             #
        #      /       _   __ _/_            (c) 2004 Eaden McKee            #
        #     /       (_(_/ (_(__            webforce.co.nz/cart             #
        #    (______)                        all rights reserved             #
        #                                                                    #
        #  Session based, Object Oriented Shopping Cart Component for PHP    #
        #                                                                    #
        ######################################################################
        # Ver 1.4 - demo included
        # Ver 1.3 - bugfix with total 
        # Ver 1.2 - added empty_cart()
        # Ver 1.0 - initial release
        You are allowed to use this script in websites you create. 
        However you may not distribute any part of this script.
        *** Instructions at [url]http://www.webforce.co.nz/cart/php-cart.php[/url] ***
        **** READ THEM!                                                 ***
        
        BUGS/PATCHES
        
        Please email [email]eaden@webforce.co.nz[/email] with any bugs/fixes/patches/comments etc.
        See [url]http://www.webforce.co.nz/cart/[/url] for updates to this script
        
        */
        class wfCart {
        	var $total = 0;
        	var $itemcount = 0;
        	var $items = array();
                var $itemprices = array();
        	var $itemqtys = array();
                var $iteminfo = array();
        
        function cart() {} // constructor function
        
        function get_contents()
        { // gets cart contents
        	$items = array();
        	foreach($this->items as $tmp_item)
        	{
        	        $item = FALSE;
        
        		$item['id'] = $tmp_item;
                            $item['qty'] = $this->itemqtys[$tmp_item];
        		$item['price'] = $this->itemprices[$tmp_item];
        		$item['info'] = $this->iteminfo[$tmp_item];
        		$item['subtotal'] = $item['qty'] * $item['price'];
                            $items[] = $item;
        	}
        	return $items;
        } // end of get_contents
        
        
        function add_item($itemid,$qty=1,$price = FALSE, $info = FALSE)
        { // adds an item to cart
                    if(!$price)
        	{
        	        $price = wf_get_price($itemid,$qty);
        	}
        
                    if(!$info)
        	{
                            $info = wf_get_info($itemid);
        	}
        
        	if($this->itemqtys[$itemid] > 0)
                    { // the item is already in the cart..
        	  // so we'll just increase the quantity
        		$this->itemqtys[$itemid] = $qty + $this->itemqtys[$itemid];
        		$this->_update_total();
        	} else {
        		$this->items[]=$itemid;
        		$this->itemqtys[$itemid] = $qty;
        		$this->itemprices[$itemid] = $price;
        		$this->iteminfo[$itemid] = $info;
        	}
        	$this->_update_total();
        } // end of add_item
        
        
        function edit_item($itemid,$qty)
        { // changes an items quantity
        
        	if($qty < 1) {
        		$this->del_item($itemid);
        	} else {
        		$this->itemqtys[$itemid] = $qty;
        		// uncomment this line if using 
        		// the wf_get_price function
        		// $this->itemprices[$itemid] = wf_get_price($itemid,$qty);
        	}
        	$this->_update_total();
        } // end of edit_item
        
        
        function del_item($itemid)
        { // removes an item from cart
        	$ti = array();
        	$this->itemqtys[$itemid] = 0;
        	foreach($this->items as $item)
        	{
        		if($item != $itemid)
        		{
        			$ti[] = $item;
        		}
        	}
        	$this->items = $ti;
        	$this->_update_total();
        } //end of del_item
        
        
            function empty_cart()
        { // empties / resets the cart
                    $this->total = 0;
                $this->itemcount = 0;
                $this->items = array();
                    $this->itemprices = array();
                $this->itemqtys = array();
                    $this->itemdescs = array();
        } // end of empty cart
        
        
        function _update_total()
        { // internal function to update the total in the cart
                $this->itemcount = 0;
        	$this->total = 0;
                    if(sizeof($this->items > 0))
        	{
                            foreach($this->items as $item) {
                                    $this->total = $this->total + ($this->itemprices[$item] * $this->itemqtys[$item]);
        			$this->itemcount++;
        		}
        	}
        } // end of update_total
        
        }
        ?>
        
        

        OK - Questions:

        Should I store my session id in a cookie, and reference that at the top of each page?
        Will that make the session more stable?
        Should I use cookies to store the items and prices?

        Any help would be brill.

        🙂

          ok well im familiar w/ wfcart.

          now, why are you using session_regenerate_id() ?????????

          i dont think thats your problem, but i also dont see why you would want to do that...... get it out of there.

          Should I store my session id in a cookie, and reference that at the top of each page? ---- no. php already does this for you.

          Will that make the session more stable? ---- no. using trans_sid will make the shopping cart work for those who dont have cookies, but your problem needs to be fixed first or nothing will ever work.

          Should I use cookies to store the items and prices? ---- big no

          now you must have some code which works w/ this class, for example when you call $cart->add_item('itemid');
          and that stuff.

          please post that code.

            OK, Here's the cart script.

            <?php 
            // Add cart tools
            include("r/wfcart.php");
            // Start the session 
            session_start();
            
            // Initalise the cart
            $cart =& $_SESSION['cart'];
            if(!is_object($cart)) $cart = new wfCart();
            
            
            // Include the settings file and the auth script 
            // as this is a private page
            include('r/mysqlsettings.php');
            
            if ($_POST['product_id'])
            {
            	$product_id = $_POST['product_id']; 
            }
            if ($_POST['quantity'])
            {
            	$quantity = $_POST['quantity']; 
            }
            if ($_POST['product_price'])
            {
            	$product_price = $_POST['product_price']; 
            }
            if ($_POST['product_info'])
            {
            	$product_info = $_POST['product_info']; 
            }
            if ($_POST['status'])
            {
            	$status = $_POST['status']; 
            }
            
            
            
            if($status == "add")
            {
            	$cart->add_item($product_id,$quantity,$product_price,$product_info);
            }
            if($status == "del")
            {
            	$cart->del_item( $product_id );
            }
            if($status == "udt")
            {
            	$cart->edit_item( $product_id, $quantity );
            }
            
            
            ?>
            <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
            "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
            <html xmlns="http://www.w3.org/1999/xhtml" lang="en"><!-- InstanceBegin template="/Templates/maintemplate.dwt" codeOutsideHTMLIsLocked="false" -->
            <head>
            <!-- InstanceBeginEditable name="title" -->
            <title>Neph Aromatics</title>
            <!-- InstanceEndEditable -->
            <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
            
            <link href="s/neph.css" rel="stylesheet" type="text/css" />
            </head>
            <body>
            
            <div id="header">
              <h1>Neph Aromatics</h1>
            </div>
            <div id="lmenu">
              <p>&nbsp;</p>
            </div>
            <div id="rmenu">
              <ul>
                <li><a href="index.php">Home</a></li>
                <li><a href="essentials.php">Essential Oils</a></li>
                <li><a href="carriers.php">Carrier Oils</a></li>
                <li><a href="products.php">Aromatherapy Products</a></li>
                <li><a href="specials.php">Special Offers</a></li>
                <li><a href="links.php">Links</a></li>
                <li><a href="contactus.php">Contact Us</a></li>
                <li><a href="cart.php">View Cart</a></li>
              </ul>
              <p>&nbsp;</p>
            </div>
            <div id="content">
              <div id="four"><!-- InstanceBeginEditable name="maincontent" -->
                <h2>Neph Aromatics Shopping Cart</h2>
                <p>You have <?php echo $cart->itemcount; ?> items in your shopping cart.</p>
                <table width="100%" border="1" cellpadding="1" cellspacing="0" bordercolor="#006600" id="cartlist">
            	  <tr>
                    <td><div class="leftalign">Item</div></td>
                    <td width="110">Remove</td>
                    <td width="150">Qty</td>
                    <td width="50">Price</td>
                    <td>Subtotal</td>
                  </tr>
                  <?php
            $items = $cart->get_contents();
            
            foreach($items as $item) {
            	//echo "Code/ID :".$item['id']."<br>";
            	echo "<tr valign=\"middle\"><td>";
            	echo "<div class=\"leftalign\">".$item['info']."</div>";
            	echo "</td>";
            ?>
                  <form method="post" name="removeproductfromcart">
                    <td valign="middle">
              <input type="image" value="Click here" alt="Remove item from Cart" src="i/remove.gif" border="0" />
              <input name="product_id" type="hidden" value="<?php echo $item['id']; ?>">
              <input name="status" type="hidden" value="del"></td>
                  </form>
                  <form method="post" name="updatecart">
                    <td valign="middle">
                        <input name="quantity" class="reallysmall" type="text" value="<?php echo $item['qty']; ?>" size="3"><br />
                        <input type="image" value="Click here" alt="Update Cart" src="i/updatecart.gif" border="0" />
                        <input name="product_id" type="hidden" value="<?php echo $item['id']; ?>">
                        <input name="status" type="hidden" value="udt">
                      </td>
                  </form>
                  <?php	
            	echo "<td valign=\"middle\">";
            		$product_price = "£" . number_format($item['price'], 2, '.', '');
            	echo "<div class=\"rightalign\">";
            	echo $product_price."</div>";
            	echo "</td><td valign=\"middle\">";
            		$total_price = "£" . number_format($item['subtotal'], 2, '.', '');
            	echo "<div class=\"rightalign\">";
            	echo $total_price."</div>";
            	echo "</td></tr>";
            	$items = $items . "
            " . $item['info'] . $item['qty'] . "at" . "=" . $total_price ;
            }
            
            ?>
            
            
              <td colspan="4"><div align="right">Total Cost&nbsp;</div></td>
              <td width="60">
                <?php $total_cost = "£" . number_format($cart->total, 2, '.', '');
            echo "<div class=\"rightalign\">";
            echo $total_cost."</div>"; ?>
                  </td>
                </table>
                <p>
                </p>
                <table width="100%" border="0" cellspacing="0" cellpadding="4">
                  <tr valign="middle">
                    <td width="90%">
            <div align="right">
                        <h2>To proceed to checkout, click here </h2>
                      </div></td>
                    <td>
            <form action="https://www.nochex.com/nochex.dll/checkout" method="post">
            
                    <input type="hidden" name="email" value="nepharomatics@blueyonder.co.uk" />
            
                    <input type="hidden" name="amount" value="<?php echo $cart->total; ?>" />
                    <input type="hidden" name="description" value="<?php echo $items; ?>" />
            
                    <input name="image" type="image" value="Click here" src="http://support.nochex.com/web/images/logoc.gif" alt="Proceed to Checkout!" border="0" />
            </form></td>
              </tr>
            </table>
            
            <p>&nbsp;</p>
            <p></p>
            <!-- InstanceEndEditable --></div>
              <div id="footer">
                <p>Neph Aromatics | 5 Whinney Field | Halifax | West Yorkshire | HX3 0NP</p>
              </div>
            </div>
            </body>
            <!-- InstanceEnd --></html>
            

              Forgot this:

                    <form action="cart.php" method="post" name="add2cart"  id="add2cart">
                      <input type="hidden" name="product_id" value="<?php echo $products_row['products_id'] ?>">
                      <input type="hidden" name="quantity" value="1">
                      <input type="hidden" name="status" value="add">
                      <input type="hidden" name="product_price" value="<?php echo $products_row['products_price'] ?>">
                      <input type="hidden" name="product_info" value="<?php echo "{$products_row['products_title']} $sections_title" ?>">
                      <input class="button" type="image" name="submit" src="i/add2cart.gif" border="0" alt="Add item to Shopping Cart.">
                    </form>
              

                i didnt see anything wrong w/ what you posted, although i must admit i didnt look very hard.

                but i noticed, your website is working now.
                you removed that session_regenerate_id() didnt you?

                i guess that was it.

                  Well, if anyones interested, I've sorted this little problem.

                  It wasn't a php related problem.

                  I've swapped the hosting to Win2003 iis server running php from a linux apache server running php. The sessions are a lot more stable.

                  Rehfeld, thanks for your help and if every you want that handbag fight, let me know 😃 😃 😃 😃 😃

                  Thanks again.

                    Write a Reply...