473,581 Members | 2,497 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

PHP Problem displaying unserialized data from cookie

5 New Member
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
Mar 6 '06 #1
4 7131
Banfa
9,065 Recognized Expert Moderator Expert
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.
Mar 6 '06 #2
MrL8Knight
5 New Member
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!!!
Mar 6 '06 #3
MrL8Knight
5 New Member
: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
Mar 7 '06 #4
henryrao
1 New Member
Thank you , MrL8Knight !

your posts save me much time !!! :D
Dec 18 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

10
19334
by: Gregory A Greenman | last post by:
I'm trying to write a program in vb.net to automate filling out a series of forms on a website. There are three forms I need to fill out in sequence. The first one is urlencoded. My program is able to fill that one out just fine. The second form is multipart/form-data. Unfortunately, I haven't been able to fill that out in a way that makes...
4
1653
by: Wally | last post by:
I have a record set (rs) that contains 25 barcodes values that I set to true in a cookie. (see code section below) If I read the cookie from within the same page that created it, I see all 25 values. But if I redirect to another page and read the cookie only 18 of the values remain. Even though I load the same 25 barcodes at login, the...
0
1724
by: Fronky | last post by:
Hope someone can help. I am still learning, so no laughing please. I am displaying records from a database using Response.Write(""); instead of the usual datagrid method. I am doing it this way to achieve the result I require. However. Since I have added a button to the screen, and have had to include the "object sender, System.EventArgs...
9
1946
by: Justin Engelman | last post by:
Hi, I have a website that uses an ISAPI filter that will redirect anyone going to any page on the site to an SSL login page (on a different website with the same root domain) if they do not have the appropriate session cookie already. After logging in on that page successfully they are given the cookie and redirected back to the page they...
6
5791
by: Coleen | last post by:
Hi all :-) I need to redirect to multiple pages on click of a transmit button, without redisplaying each page. This redirection is to capture session variables that are created on each page and pass them to the main page to be displayed. We are actually NOT using session variables, but storing the values in a temporary table. The problem...
5
1686
by: sophie_newbie | last post by:
Does anyone know how to do this? I can't seem to make it work. I'm using: c = Cookie.SimpleCookie() c = "unamepwordwhatever" c.expires = time.time() + 300 print c
5
4109
by: bgold12 | last post by:
When a user logs onto my site, I set some session data using php: .... session_start(); .... $_SESSION = 'value'; .... From what I understand, this should store the session data (i.e. the key-value pairs) in a cookie on the user's computer, and it should be
9
2284
by: LayneMitch via WebmasterKB.com | last post by:
Hello. Got another one for you folks. I'm working on this problem that wants me to 1. Prompt for name 2. Use pop-up box with name 3. Display current date on page in format "October 30, 2000." 4. Display last modified date of doc. Here is my attempt. What a headache :-(
10
2175
by: lenzie | last post by:
I am having a very odd problem with a website I run. The menus have recently been changed to use javascript and some people are reporting that when they first use the site, Instead of a nicely aligned list of items each in its own light-blue box. They are seeing a straightforward <li> list with no styles. The biggest problem is that I think it...
0
7868
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8304
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7899
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6553
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5674
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5364
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3805
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3827
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2301
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.