Session

Hi,

Im really new to php and stuff…But as my Final year project Im developing a shoppingcart system.But there is an error which I could not figure out …If you can plzz help me…There for anyone who logged it will display the same cart…I think problem is cz i hve set a cookie instead of a session…But Im unable to correct it…Here Im sending you my code plz correct it and help me…Im totally stucked with this issue… :frowning:

shopcart.php

<?php session_start(); if(!isset($_SESSION['valid']) || $_SESSION['valid']!="yes") { header("location:index.php"); //print "invalid login"; exit(); } ?> <!-- function UpdateQty(item) { itemId = item.name; newQty = item.options[item.selectedIndex].text; document.location.href = 'shopcart.php?action=update_item&id='+itemId+'&qty='+newQty; } </script> <?php include_once "../connect.php"; //$myusername = $_SESSION['user_name']; if(isset($_GET["action"])) { switch($_GET["action"]) { case "add_item": { AddItem($_GET["id"], $_GET["qty"]); ShowCart(); break; } case "update_item": { UpdateItem($_GET["id"], $_GET["qty"]); ShowCart(); break; } case "remove_item": { RemoveItem($_GET["id"]); ShowCart(); break; } case "show_cart": { ShowCart(); } } } function AddItem($itemId, $qty) { $result = mysql_query("SELECT COUNT(*) FROM tbl_cart WHERE cookie_id = '" . GetCartId() . "' AND item_id =$itemId") or die(mysql_error()); $row = mysql_fetch_row($result); $numRows = $row[0]; if($numRows == 0) { // This item doesn't exist in the users cart,we will add it with an insert query mysql_query("INSERT INTO tbl_cart(cookie_id,item_id, qty) VALUES('" . GetCartId() . "', $itemId, $qty)") or die(mysql_error()); } else { // This item already exists in the users cart,we will update it instead UpdateItem($itemId, $qty); } } function UpdateItem($itemId, $qty) { mysql_query("UPDATE tbl_cart SET qty = $qty WHERE cookie_id = '" . GetCartId() . "' AND item_id = $itemId") or die(mysql_error()); } function RemoveItem($itemId) { mysql_query("DELETE FROM tbl_cart WHERE cookie_id = '" . GetCartId() . "' AND item_id = $itemId") or die(mysql_error()); } function ShowCart() { $result = mysql_query("SELECT * FROM tbl_cart INNER JOIN tbl_product ON tbl_cart.item_id = tbl_product.prod_id WHERE tbl_cart.cookie_id = '".GetCartId()."' ORDER BY tbl_product.prod_name ASC") or die(mysql_error()); //echo("
"); $totalCost=0; while($row = mysql_fetch_array($result)) { // Increment the total cost of all items $totalCost += ($row["qty"] * $row["prod_price"]) ; ?>
<tr>
    <th width="220" height="25" >Product Name & Quantity</th>
    <th width="173" height="25" >Product Price</th>
    <th width="77" height="25" >Remove</th>
</tr>
<tr>
    <td width="220" height="25" align="center">
        <?php echo $row["prod_name"]; ?>        
        <select name="<?php echo $row["item_id"]; ?>" onChange="UpdateQty(this)">
        <?php
                for($i = 1; $i <= 20; $i++)
                {
                    echo "<option ";
                        if($row["qty"] == $i)
                        {
                            echo " SELECTED ";
                        }
                            echo ">" . $i . "</option>";
                }
        ?>
        </select>
   </td>
   <td width="173" height="25" align="center">
          Rs.<?php echo number_format($row["prod_price"], 2, ".", ","); ?>    
    </td>
    <td width="77" height="25" align="center">
        <a href="shopcart.php?action=remove_item&amp;id=<?php echo $row["item_id"]; ?>">Remove</a>        
    </td>
</tr>
<?php
    }
?>
<tr>
    <td colspan="4">
        <hr size="1" color="red" NOSHADE>      
    </td>
</tr>
<tr>
     <td colspan="2"><a href="showroom.php">&lt;&lt; Keep Shopping </a> &nbsp;&nbsp; 
     <a href="checkout.php">Checkout &gt;&gt;</a></td>
    <td width="77" colspan="2" align="center">
        <b>Total: Rs. <?php if(isset($totalCost)) { echo number_format($totalCost, 2, ".", ","); } else print "Rs. 0.00"; ?></b>
    </td>
    <?php
        }
    ?>

connect.php

<?php $host="localhost"; // Host name $username="root"; // Mysql username $password=""; // Mysql password $db_name="Lakkam"; // Database name // Connect to server and select databse. $conn=mysql_connect("$host", "$username", "$password")or die(mysql_error()); mysql_select_db("$db_name")or die(mysql_error()); function GetCartId() { // This function will generate an encrypted string and will set it as a cookie using set_cookie. This will also be used as the cookieId field in the cart table if(isset($_COOKIE["cart_id"])) { return $_COOKIE["cart_id"]; } else { // There is no cookie set. We will set the cookie and return the value of the users session ID @session_start(); setcookie("cart_id",session_id(), time() + (3600 * 24)); //cookie is set return session_id(); } } ?>

I think problem might be in your login/logout script. When you’re logging out, you need to delete cookie cart_id and also terminate session. I believe you’re testing from the same computer/browser under different user login, because otherwise situation when session id is the same for different users is rarely possible :slight_smile:

Sponsor our Newsletter | Privacy Policy | Terms of Service