PHP Problem displaying unserialized data from cookie

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • MrL8Knight
    New Member
    • Mar 2006
    • 5

    PHP Problem displaying unserialized data from cookie

    Hello,
    I am trying to build a simple php form based shopping cart using a cookie with arrays. I need to use 1 cookie because each order will have over 20 items. With that said, I realize I need to serialize the data to put the array into the cookie. That part of my code is working just fine and displaying fine. The problem I’m having is when I try to unserialize and display; the data does not appear. If I remove my unserialize command line (see page 3) the data displays just fine with the serialization added to it.

    So basically I need help unserializing and displaying the data so I can display it in my shopping cart nice and clean.

    Here is page 1(index2.php) which is a form that asks for opening name, width and height.

    <?php
    if (isset($_COOKIE['count'])) {
    $count = $_COOKIE['count'] + 1;
    } else {
    $count = 1;
    }
    setcookie('coun t', $count, time()+3600);
    setcookie("Cart[$count]", $item, time()+3600);
    echo "$count";

    echo "<form action='add.php ?id=$count' method='post'>
    <input name='name' type='hidden' value='$count'>
    Opening name<br>
    <input name='name' type='text' size='30'>
    </b></p>
    <p><b> Width<br>
    <input name='w' type='text' size='30'>
    </b></p>
    <p><b> Height<br>
    <input name='h' type='text' size='30'>
    </b></p>
    <p>
    <input type='image' src='../images/Start-Order.gif' name='submit' alt='Start Order'>
    <br>
    </p>
    </form>";
    ?>



    The second page(add.php) receives the data, creates a serialized array and adds it to the cookie just fine.


    <?php
    $count= $_GET['id'];
    $name= $_POST['name'];
    $w= $_POST['w'];
    $h= $_POST['h'];

    $serialized_dat a = serialize (array ($count, $name, $w, $h));

    setcookie("myco okie[$count]", $serialized_dat a, time()+13600);

    echo "Opening was Added<br><br>";
    echo "$count";
    echo "<br><br>";
    echo "$name";
    echo "$w";
    echo "$h";
    echo "<br><br>";

    echo "<a href='index2.ph p'>Add another opening!</a><br>";
    echo "<a href='show.php' >Show!</a><br>";

    ?>

    Where the problem is when you click show (show.php) you see the serialized data.

    I am trying to restore the data with an unserialize (see below line 8), but when I add that line of code, my data disappears instead of being stripped of the serialization. When I remove line 8, the data displays serialized.

    So something is wrong, I guess with my unserialize method.


    <?php

    if($_COOKIE["mycookie"]) {

    foreach( $_COOKIE[mycookie] as $key => $value)
    {

    $value2 = unserialize ($value);

    echo "Number $key : Description $value2";

    }
    echo "<a href='index2.ph p'>Add Opening</a><br><br>";

    }
    else {
    print "No Items in your cart<br><br>";
    echo "<a href='index2.ph p'>Add Opening</a><br><br>";
    }

    ?>



    I have tried all weekend long and could use some help! Thanks
    -Mr L8Knight
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    You have not provide sample input and output data (particularly value and value2 from page 3) which complicates the matter of diagnosing the problem.

    However I can't help noticing that page 2 you serialize an array but on page 3 having unserialized $value2 you do not treat it like an array at all.

    Comment

    • MrL8Knight
      New Member
      • Mar 2006
      • 5

      #3
      Here is the serialized version and data it displays

      Thank you for your response!

      This is the code I am using for page 3. It is working just fine, but is displaying the data serialized.

      <?php

      echo "<br><br>";

      if($_COOKIE["mycookie"]) {


      foreach( $_COOKIE[mycookie] as $key => $value)
      {

      echo "Number $key : Description $value<br>";


      }
      echo "<a href='index2.ph p'>Add Opening</a><br><br>";

      }
      else {
      print "No Items in your cart<br><br>";
      echo "<a href='index2.ph p'>Add Opening</a><br><br>";
      }


      ?>


      Here is what the data looks when outputted:

      Number 1 : Description a:4:{i:0;s:1:\" 1\";i:1;s:6:\"t ester\";i:2;s:2 :\"12\";i:3;s:2 :\"12\";}
      Number 2 : Description a:4:{i:0;s:1:\" 2\";i:1;s:7:\"t ester2\";i:2;s: 2:\"48\";i:3;s: 2:\"48\";}
      Add Opening


      Maybey it would be easier to show me how I would go about unserializing the data from the code above in this post.

      I have tried everything I could find on the net and still no luck, I'm sure this is something really simple to a pro!

      Thanks!!!

      Comment

      • MrL8Knight
        New Member
        • Mar 2006
        • 5

        #4
        Problem Solved!!!

        :D
        Ok I finally figured this one out, first thank you to all that have helped me with this problem; I can’t express how much it is appreciated.

        Here are the 3 working pages, hope someone else finds this simple, PHP form based shopping cart usefull!

        Page 1 (index2.php)(th e Form, which starts a cookie to keep an incremental count (Key) for each opening)

        <?php
        if (isset($_COOKIE['count'])) {
        $count = $_COOKIE['count'] + 1;
        } else {
        $count = 1;
        }
        setcookie('coun t', $count, time()+13600);
        echo "$count";

        echo "<form action='add.php ?id=$count' method='post'>
        <input name='name' type='hidden' value='$count'>
        Opening name<br>
        <input name='name' type='text' size='30'>
        </b></p>
        <p><b> Width<br>
        <input name='w' type='text' size='30'>
        </b></p>
        <p><b> Height<br>
        <input name='h' type='text' size='30'>
        </b></p>
        <p>
        <input type='image' src='../images/Start-Order.gif' name='submit' alt='Start Order'>
        <br>
        </p>
        </form>";
        ?>


        Page 2 (add.php)(the script to get and serialize data typed in the form and add it to the cookie)


        <?php
        $count= $_GET['id'];
        $name= $_POST['name'];
        $w= $_POST['w'];
        $h= $_POST['h'];

        $serialized_dat a = serialize (array ($count,$name,$ w,$h));

        setcookie("myco okie[$count]", $serialized_dat a, time()+13600);

        echo "Opening was Added<br><br>";
        echo "$count";
        echo "<br><br>";
        echo "$name";
        echo "$w";
        echo "$h";
        echo "<br><br>";

        echo "<a href='index2.ph p'>Add another opening!</a><br>";
        echo "<a href='show.php' >Show Cart!</a><br>";

        ?>


        Page 3 (show.php)(the show cart page that checks to see if items are in the cart (or cookie) and if so, shows the unserialized data)

        <?php

        if($_COOKIE["mycookie"]) {

        foreach( $_COOKIE[mycookie] as $key => $value)
        {

        $value2 = unserialize(str ipslashes($valu e));
        echo "<br>";
        print_r($value2[0]);
        print_r($value2[1]);
        print_r($value2[2]);
        print_r($value2[3]);

        }
        echo "<br><br>";
        echo "<a href='index2.ph p'>Add Opening</a><br><br>";

        }
        else {
        print "No Items in your cart<br><br>";
        echo "<a href='index2.ph p'>Add Opening</a><br><br>";
        }

        ?>

        Thanks again everyone… Cheers!

        -MrL8Knight

        Comment

        • henryrao
          New Member
          • Mar 2007
          • 1

          #5
          Thank you , MrL8Knight !

          your posts save me much time !!! :D

          Comment

          Working...