Jump to content

Sessions not working correctly? why?


gibbon25

Recommended Posts

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;
}
?>

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

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.