becca (becala) wrote in php,

session variable help? : SOLVED, but totally bizarre

So I had a session variable that was mysteriously auto-incrementing itself. Original post behind a cut for posterity/curiosity. The solution?

The code was within a template that was handed to me by someone else. Therefore, I did not examine it carefully, just used includes to insert my own code. In his css, he had background image: url(). I'm guessing that he was using dreamweaver or somesuch and originally had a background image, then deleted it.

I have absolutely no idea how this little piece of css caused the problem, but there it is. If I add a url such as background image: url(images/spacer.gif) or deleted the css piece entirely, everything works as expected.

I am shocked and confused as well as relieved.

Context: I'm working on a bare-bones shopping cart system and am using a session variable $cart to store cart items. $cart is an associative array holding item_id => quantity. To add an item to the cart, I'm calling the page with a url variable ?new=xx where xx is the item id. If the item id exists in the array, am incrementing it. If it doesn't exist, I create it and set the quantity to 1. In theory, anyway.

Problem: Adding a new item to the cart produces the expected results, but if the page is called immediately after without the url variable, or with a url variable pointing to a different id, it increments the quantity of the item just added by one. It is also the case that when I call a separate checkout page with the display_cart($cart) function in it, the item quantity shows up incremented by one.

(the following code will look very familiar if you've ever used PHP and MySQL Web Development)

On the shopping cart page:

        $cart = array();

        $cart[$new] = 1;

if($save) //have tried commenting out this part, makes no difference
    foreach ($cart as $item_id => $qty)
        if ($$item_id == "0")
            $cart[$item_id] = $$item_id;

if ($cart&&array_count_values($cart))
    echo "There are no items in your cart.";
Display cart function:

function display_cart($cart)
    echo "<form action = show_cart.php method = post>";
    echo "<table border=1><tr>";
    echo "<td>item name</td>";
    echo "<td>qty</td>";
    echo "<td>price</td></tr>";

    foreach($cart as $item_id => $qty)
        $item = get_item_details($item_id);
        echo "<tr><td>";
        echo $item['name'];
        echo "</td><td>";
        echo "<input type = text name = \"$item_id\" value = $qty size = 3>";
        echo "</td><td>";
        echo $item['price'];
        echo "</td></tr>";

    echo "</tr>";
    echo " <tr><td>&nbsp;</td>
    < td align = center >
    < input type = hidden name = save value = true >
    < input type = image src = \"images/save_changes.jpg\" border = 0 alt = \"Save Changes\">
    echo "</table>";
    echo "</form>";

I've done a bit of reading on how sessions work, but am still pretty confused by it. Is this black magic or am I just not going about this right? Any suggestions, including debugging techniques, would be appreciated. In the meantime, I'm just going to take a whole different approach to this cart, but I'd like to solve the mystery eventually.

Thanks in advance.
  • Post a new comment


    default userpic

    Your reply will be screened

    Your IP address will be recorded 

    When you submit the form an invisible reCAPTCHA check will be performed.
    You must follow the Privacy Policy and Google Terms of use.
  • 1 comment