0

having a problem with storing an array variable as a cookie, then adding in separate arrays into the cookie already containing the array

$mysqli = mysqli_connect($db_host,$db_user,$db_pass,$db_base);
        if (mysqli_connect_errno()) 
        {
            printf("Connect failed: %s\n", mysqli_connect_error());
            exit();
        }
        // Displays a message saying product was added to the basket
        $message = $_POST['product_name']." was added to the basket";
        echo "<script>alert(".$message.")</script>";

        // Sets the basket
        $itemID = $_POST['product_id'];
        $itemQuantity = $_POST['product_quantity'];
        if ($itemQuantity > 0)
        {
            $items = [$itemID, $itemQuantity];

            // Returns cookie value as array
            $basket_array = (unserialize($_COOKIE['eg_basket']));

            // Adds to array into cookie array
            $basket = serialize(array_push($basket_array, $items));

            // Sets basket back as cookie
            setcookie('eg_basket', $basket); // will expire on browser close

            // Displays message
            echo "<h3 style='text-align:center'>".$_POST['product_name']." was added to the basket</h3>";
            echo "<br/>";
            echo "<p style='text-align:center'>Please click below to return to the previous page</p>";
        }
        else
        {
            echo "<h3 style='text-align:center'>ERROR: ".$_POST['product_name']." was not added to the basket, invalid quantity given</h3>";
        }

            echo "<form method='POST' action='product_info.php'><input style='display:none' type='number' name='product_id_POST' value='".$_POST['product_id']."'><input style='text-align:center' type='submit' value='Return'></form>";


        // close the connection
        mysqli_close($mysqli);
    ?>

What i am tryng to do is to create a cookie that will store arrays of product id;s and the quantity

e.g. cookie = [[product_id, quantity],[product_id, quantity],.......]; however

im pretty sure this is not the case, but this is the code i am using to create the basket cookie if it does not exist (could this be the reason why the cookie will not accept any new values

// Checks is cookie is already set - basket only
    if (!isSet($_COOKIE['eg_basket']))
    {
        $basket = serialize([]);
        setcookie('eg_basket', $basket); // will expire on browser close
    }

Thanks, any help is greatly appreciated

Bull

1
  • 2
    i suggest you to change from cookie to session, would be easier to manage and more secure ^_^ Commented Dec 9, 2013 at 16:36

1 Answer 1

1

You cannot send a cookie to the browser when the headers already have been sent, see the manual.

So you need to log your message without echoing anything here:

echo "<script>alert(".$message.")</script>";

And make sure no other output is sent to the browser before your setcookie() line.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.