gibbon25 Posted September 7, 2010 Share Posted September 7, 2010 Hi, i am trying to add a very simple shopping cart script to my site, and the session is simply used to keep the contents of the shopping cart, yet its not working properly! Ok ignoring the layout issues with the cart here is the issues. Only two links to the same script First one is a link to the shopping cart with an action to add the item ID to it. http://www.heliuk.co.uk/index.php?n=pages/shop-cart-action&id=1&action=add This works, it adds the item to the session "cart", if that's successful it then shows the cart from the session. But if i just go to the shopping cart with no actions (so just to view it) it shows no items http://heliuk.co.uk/index.php?n=pages/shop-cart-action And what this is doing is checking it the cart is empty, if not show the cart, if it is then show nothing, which is what happening. But if you go back to the first link and add the item again, it is adding it on to the cart so the session is there and working? I don't understand what's happening! Here the shopping cart page. <?php error_reporting(E_ALL ^ E_NOTICE); $product_id = $_GET["id"]; //the product id from the URL $action = $_GET["action"]; //the action from the URL //if there is an product_id and that product_id doesn't exist display an error message if($product_id && !productExists($product_id)) { die("Error. Product Doesn't Exist"); } switch($action) { //decide what to do case "add": $_SESSION['cart'][$product_id]++; //add one to the quantity of the product with id $product_id break; case "remove": $_SESSION['cart'][$product_id]--; //remove one from the quantity of the product with id $product_id if($_SESSION['cart'][$product_id] == 0) unset($_SESSION['cart'][$product_id]); //if the quantity is zero, remove it completely (using the 'unset' function) - otherwise is will show zero, then -1, -2 etc when the user keeps removing items. break; case "empty": unset($_SESSION['cart']); //unset the whole cart, i.e. empty the cart. break; } ?> <table class='main' cellspacing='1' cellpadding='4'> <tr class='head'> <td class='head' colspan='2'>HeliUK Shop - Your Shopping Cart</td> </tr> <tr> <td style='Text-align:left;' class='con1' colspan='2'> <table border="0" cellpadding="0" cellspacing="0" width="100%"> <?php error_reporting(E_ALL ^ E_NOTICE); if($_SESSION['cart']) { //if the cart isn't empty //show the cart echo "<table border=\"1\" padding=\"0\" width=\"100%\">"; //format the cart using a HTML table //iterate through the cart, the $product_id is the key and $quantity is the value foreach($_SESSION['cart'] as $product_id => $quantity) { //get the name, description and price from the database - this will depend on your database implementation. //use sprintf to make sure that $product_id is inserted into the query as a number - to prevent SQL injection $sql = sprintf("SELECT title, description, price FROM items WHERE id = %d;", $product_id); $result = mysql_query($sql); //Only display the row if there is a product (though there should always be as we have already checked) if(mysql_num_rows($result) > 0) { list($name, $description, $price) = mysql_fetch_row($result); $line_cost = $price * $quantity; //work out the line cost $total = $total + $line_cost; //add to the total cost echo "<tr>"; echo "<td style=\"border-left-width: 1px; border-right-style: solid; border-right-width: 1px; border-top-width: 1px; border-bottom-style: solid; border-bottom-width: 1px\" width=\"145\">"; echo "<font face=\"Tahoma\" size=\"2\">$quantity</font></td>" ; echo "<td style=\"border-left-width: 1px; border-right-style: solid; border-right-width: 1px; border-top-width: 1px; border-bottom-style: solid; border-bottom-width: 1px\" width=\"693\">"; echo "<font face=\"Tahoma\" size=\"2\">$name</font></td>" ; echo "<td style=\"border-left-width: 1px; border-right-style: solid; border-right-width: 1px; border-top-width: 1px; border-bottom-style: solid; border-bottom-width: 1px\" width=\"162\">"; echo "<font face=\"Tahoma\" size=\"2\">$line_cost</font></td>" ; echo "<td style=\"border-left-width: 1px; border-right-width: 1px; border-top-width: 1px; border-bottom-style: solid; border-bottom-width: 1px\">"; echo "<p align=\"center\"><a href=\"$_SERVER[php_SELF]?action=remove&id=$product_id\">" ; echo "<img border=\"0\" src=\"empty-cart.jpg\" width=\"24\" height=\"24\"></a></td>" ; echo "</tr>"; } } //show the total echo "<tr>"; echo "<td colspan=\"0\" align=\"right\">Total</td>"; echo "<td align=\"right\">$total</td>"; echo "</tr>"; //show the empty cart link - which links to this page, but with an action of empty. A simple bit of javascript in the onlick event of the link asks the user for confirmation echo "<tr>"; echo "<td colspan=\"0\" align=\"right\"><a href=\"$_SERVER[php_SELF]?action=empty\" onclick=\"return confirm('Are you sure?');\">Empty Cart</a></td>"; echo "</tr>"; echo "</table>"; }else{ //otherwise tell the user they have no items in their cart echo "You have no items in your shopping cart."; } ?> </table> </td> </tr> </table> <? //function to check if a product exists function productExists($product_id) { //use sprintf to make sure that $product_id is inserted into the query as a number - to prevent SQL injection $sql = sprintf("SELECT * FROM items WHERE id = %d;", $product_id); return mysql_num_rows(mysql_query($sql)) > 0; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/212753-sessions-not-working-correctly-why/ Share on other sites More sharing options...
Alex Posted September 7, 2010 Share Posted September 7, 2010 session_start needs to be at the top of every page that uses the $_SESSION array. Quote Link to comment https://forums.phpfreaks.com/topic/212753-sessions-not-working-correctly-why/#findComment-1108215 Share on other sites More sharing options...
gibbon25 Posted September 7, 2010 Author Share Posted September 7, 2010 Ok, ive just tested it and its bl**dy workng now!! Can anyone just confirm this is working for them? Why would it have not been doing? And the session_start() is always at the top of the template page. Thanks Andy Quote Link to comment https://forums.phpfreaks.com/topic/212753-sessions-not-working-correctly-why/#findComment-1108218 Share on other sites More sharing options...
gibbon25 Posted September 7, 2010 Author Share Posted September 7, 2010 Still not working correctly. I have completly lost my mind on this? Such a simple thing, yet it snot working? Andy Quote Link to comment https://forums.phpfreaks.com/topic/212753-sessions-not-working-correctly-why/#findComment-1108219 Share on other sites More sharing options...
gibbon25 Posted September 7, 2010 Author Share Posted September 7, 2010 Ok, this is crazy. I have it not at the moment, if i add items, it adds them accordingly and correctly and shows the cart correctly. If i click to see the shopping cart with no actions its showing a previous shopping cart? Even though its the same script and same session? But its showing to different carts depending on the way i GOTO the shopping cart? why why why? Thanks Andy Quote Link to comment https://forums.phpfreaks.com/topic/212753-sessions-not-working-correctly-why/#findComment-1108228 Share on other sites More sharing options...
PFMaBiSmAd Posted September 7, 2010 Share Posted September 7, 2010 Based on the URL's you posted (one with www. and one without), you are switching the hostname/subdomain name and I don't see anywhere in your code where you are setting the session.cookie_domain setting to match all variations of your domain name. Ref: http://www.php.net/manual/en/session.configuration.php#ini.session.cookie-domain Quote Link to comment https://forums.phpfreaks.com/topic/212753-sessions-not-working-correctly-why/#findComment-1108243 Share on other sites More sharing options...
gibbon25 Posted September 7, 2010 Author Share Posted September 7, 2010 Nail on the head! This was driving me crazy. Thanks Andy Quote Link to comment https://forums.phpfreaks.com/topic/212753-sessions-not-working-correctly-why/#findComment-1108275 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.