PHP cart with cookies only

Associate
Joined
19 Jul 2006
Posts
1,847
Bit stumped with this, I have to make a shopping cart that uses cookies not sessions. Ever tutorial I have found uses sessions for this. Does anyone know of a tutorial that just uses cookies.
Or is it possible to more or less just swap the session for cookie?
 
I'd always thought using the cookie for real data was pretty insecure... that's why you store the unique session ID in the cookie, and everything else in the session. :o

Is this for college or something? Maybe it's an exercise to show the advantages and disadvantages of storing data in this way...
 
yeah its for a course im doing. otherwise id have done it as a session. But were not allowed to do sessions for this assignment
 
Create an array to store everything you want to keep in your cookie then serialize it before saving it as cookie data.

PHP:
// save data
$fakeSession = array();
$fakeSession['foo'] = 50;
$fakeSession['bar'] = 'Something';

$data = serialize($fakeSession);
setcookie('cookiename', $data, ... etc);

// get data
$fakeSession = unserialize($_COOKIE['cookiename']);
print $fakeSession['foo']; // prints 50

Wrap that in some functions and you get something similar to sessions.
 
sorry but i cant get that to work
PHP:
<?php 
$fakeSession = array();
$fakeSession['foo'] = 50;
$fakeSession['bar'] = 'Something';
$data = serialize($fakeSession);
setcookie("item",$data,time()+3600);?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
	

</head>
<body>

	<?php $fakeSession = unserialize($_COOKIE['item']);
print $fakeSession['foo'];?>
	<p>No cart available.</p>
	

</body>
</html>

I run this page and then reload it just to make sure that the cookies are set but nothing, all i get is "no cart available"
 
Last edited:
Ah, change the unserialize to:
PHP:
$fakeSession = unserialize(stripslashes($_COOKIE['item']));

Although, a better way to do this to avoid problems with stripslashes would be:
PHP:
// set the data
$data = base64_encode(serialize($fakeSession));
set_cookie('item', $data, ...);

// get the data
$fakeSession = unserialize(base64_decode($_COOKIE['item']));
 
Last edited:
Remember though that cookies have a fairly small limit to the amount of data that can be stored, so also look at adding a chunking method to split data across multiple cookies.

Edit: *facepalm* @ Steve Gibson - going on about basically re-inventing the ASP.NET _VIEWSTATE hidden field.
 
Last edited:
Im getting there but now i have a problem with associated arrays.
PHP:
<?php
@$new=$_POST['item'];
if($new) {
if(!isset($_COOKIE["cart"])){
$Scart = array();
$Scart[$new] = "1";
$serial = serialize($Scart);
setcookie("cart","$serial",time()+100000);
}
if(isset($_COOKIE["cart"])){
$Scart = array();
$Scart = unserialize(stripslashes($_COOKIE["cart"]));
$Scart[$new] = "1";
$serial = serialize($Scart);
setcookie("cart","$serial",time()+100000);
}
}
?>

to get output im using
PHP:
print_r (unserialize(stripslashes($_COOKIE["cart"])));
Which gives Array ( [tshirt] => 1 [jumper] => 1 )

So I create an array with the Scart with a entry "item" which is the name of the product been sent from the form on previous page and set it to 1 (quantity)

But now i need to put this output into a table with |product|Quantity(that can be changed| and update.

so how do i get each product in its own row and how do i search through the variable to be able to update the quantity later?

TIA
 
Alternatively, you could store all the cart data etc in a database, and just store the cart session in the cookie which you use to grab all the data.
 
Not allowed to make any more tables in the database to store the cart data.
I can do the update now but still having trouble with the associated arrays
 
Back
Top Bottom