Forum Moderators: coopster

Message Too Old, No Replies

basic cookie-less shopping cart

         

lindajames

4:52 pm on Jul 23, 2003 (gmt 0)

10+ Year Member



Hi,

i need a very basic PHP based cookie-less shopping cart that doesnt use databases and works with Windows IIS. I dont have database support on my site so the cart cant use databases, but it can use flat-text file databases if it has to.

i am currently using a javascript based shopping cart called NOP CART available at www.nopdesign.com/freecart but the problem is i cannot use it anymore as alot of my customers are students and they use college computers to order, and alot of colleges turn off their cookies and as a result my customers cannot order and receive alot of emails saying their shopping cart shows empty.

any help would be very much appreciated.

many thanx

cheers
linda

vincevincevince

4:56 pm on Jul 23, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



you need to use php sessions - that way if a cookie can't be sent then php will automatically add the session ID to your URL :-)

[php.net...]

lindajames

5:04 pm on Jul 23, 2003 (gmt 0)

10+ Year Member



hmmmm, isnt there any ready made scripts out there that use sessions?

mat_bastian

5:06 pm on Jul 23, 2003 (gmt 0)

10+ Year Member



s'all about digging in up to your elbows and learnin' it lindajames.

That or you could pay a programmer to give you a hand.

vincevincevince

7:08 pm on Jul 23, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



linda_james - i would expect all the better e-commerce shopping cart suites will use sessions - but i wouldn't expect them to be at all cheap.

like mat says - you got to get your hands dirty and do some coding - if you use other people's code you end up spending longer getting your code to work with theirs and their code to do what you want not what they wanted than you would have ever spent writing what you needed straight off - and you'd still have worries about security and capability which you'd not have if you had coded it by hand.

ksponline

4:15 am on Jul 24, 2003 (gmt 0)

10+ Year Member



I've been looking for the same thing too (actually simple php shopping cart that doesn't use a database) without any luck.

Plan A was to have a php solution, but I'm ending up having to use a javascript/php combination (Plan B) until I get more experience with php, sessions, and arrays.

I had a link to something to post but it's Perl and I just noticed it's also for Unix... If I run across anything, I'll post here.

Good luck.

kelly_boyce

10:28 am on Aug 13, 2003 (gmt 0)

10+ Year Member



i am also desperately looking for something similar. i've found one called cartman.

but problem with cartman is it uses cookies and doesnt support shipping costs. the good thing about cartman is that it does not requires third-party database-systems, such as MySQL.

if anyone has any suggestions, plzzzzz let me know.

many thanx in advance

[edited by: jatar_k at 4:23 pm (utc) on Aug. 13, 2003]
[edit reason] no urls thx [/edit]

kelly_boyce

9:47 pm on Aug 13, 2003 (gmt 0)

10+ Year Member



doesnt anyone know of any simple carts as such? im looking for something very simple, or if someone can code one for me I am willing to pay for it, please sticky mail me the price etc.

thanx in advance
kelly

waitman

5:46 am on Aug 14, 2003 (gmt 0)

10+ Year Member



hmmm, i think this will work. please note individual files.

layout.html
------------------------------------------------

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
<title>Cheap Chickie Cart By Waitman</title>
<style type="text/css">
body { font-family: verdana; font-size: 12px; color: #5c5c5c; }
td { font-size: 12px; }
.alt { background-color: #eeeeee; }
.alt td { border-bottom: 1px solid #cccccc; border-top: 1px solid #cccccc; }
.bone { border: 2px solid #cccccc; }
</style>
</head>

<body>
<h3>Cheaper Cart</h3>
<p><a href="index.php?id=<!--id-->">home</a> ¦ <a href="view.php?id=<!--id-->">view</a></p>
<!--Content-->

</body>
</html>

------------------------------------------------
index.php
------------------------------------------------

<?php

//index.php - show a product to add
$id = base64_decode($_GET['id']);
if (strlen($id)!=32)
{
$id=md5(time().rand(99,9999999));
}
$price='43.32';
$sku='1553';
$checksum=md5($sku.$price.'xxxtopsecret');

$content = '<form method="post" action="add.php?id='.urlencode(base64_encode($id)).'"><input type="hidden" name="price" value="'.$price.'"><input type="hidden" name="checksum" value="'.$checksum.'"><input type="hidden" name="sku" value="'.$sku.'"><select name="style"><option value="snake skin">snake skin</option><option value="booty">booty</option><option value="right one">right one</option></select><select name="size"><option value="xsm">xsm</option><option value="sm">sm</option><option value="m">m</option><option value="xm">xm</option><option value="xxx">xxx</option></select><select name="color"><option value="green">green</option><option value="yellow">yellow</option><option value="orange">orange</option></select><select name="qty"><option value="1">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option><option value="5">5</option><option value="6">6</option></select><input type="submit" name="submit" value="add to cart"></form>';

$layout=join('',file('layout.html'));
$html=str_replace('<!--Content-->',$content,$layout);
$html=str_replace('<!--id-->',$id,$html);
echo $html;
?>

------------------------------------------------
add.php
------------------------------------------------

<?php

$id = base64_decode($_GET['id']);
if (strlen($id)!=32)
{
$content = 'please quit messing with the cart id, sirmam.';
} else {
$cart=unserialize(base64_decode(@join('',@file('carts/'.$id))));
$ym=array();
$checksum=md5($_POST['sku'].$_POST['price'].'xxxtopsecret');
if ($checksum!= $_POST['checksum'])
{
$content = 'bad checksum. goobye';
} else {
$ym['sku']=$_POST['sku'];
$ym['price']=$_POST['price'];
$qty=intval($_POST['qty']);
$ym['style']=$_POST['style'];
$ym['size']=$_POST['size'];
$ym['color']=$_POST['color'];
$id=md5($_POST['sku'].$_POST['style'].$_POST['size'].$_POST['color']);
if ($qty>0)
{
$cart[$id]['qty']+=$qty;
$cart[$id]['item']=base64_encode(serialize($ym));
$fp=fopen('carts/'.$id,'w');
fwrite($fp,base64_encode(serialize($cart)));
fclose($fp);
$content = 'Item Added. Thank You.';
// {or} header('Location: view.php');
} else {
$content = "You didn't pick a thing to add.";
}
}
}
$layout=join('',file('layout.html'));

$html=str_replace('<!--Content-->',$content,$layout);
$html=str_replace('<!--id-->',urlencode(base64_encode($id)),$html);
echo $html;
?>

------------------------------------------------
view.php
------------------------------------------------

<?php

//view.php - view your cart

$id = base64_decode($_GET['id']);
if (strlen($id)!=32)
{
$content = 'Your bag is obviously empty.';
} else {
$cart=unserialize(base64_decode(@join('',@file('carts/'.$id))));
$content = '<p><strong>Your Bag.</strong></p><table border="0" cellspacing="0" cellpadding="10"><tr><td class="bone"><table border="0" cellspacing="0" cellpadding="10"><td align="right"><strong>#</strong></td><td><strong>Item</strong></td><td align="right"><strong>Qty</strong></td><td align="right"><strong>Unit</strong></td><td align="right"><strong>Amount</strong></td><td>&nbsp;</td></tr>';
$counter=1;
$alt=true;
$total=0.0;
foreach ($cart as $key=>$value)
{
$qty = $value['qty'];
$item = unserialize(base64_decode($value['item']));
if ($alt)
{
$content .= '<tr class="alt" valign="top">';
$alt=false;
} else {
$content .= '<tr valign="top">';
$alt=true;
}
$content .= '<td align="right">'.$counter.'</td><td><strong>'.$item['sku'].'</strong><br><small><em>style:</em> '.$item['style'].' <em>size:</em> '.$item['size'].' <em>color:</em> '.$item['color'].'</small></td><td align="right">'.$qty.'</td><td align="right">$'.number_format($item['price'],2,'.','').'</td><td align="right">$'.number_format($item['price']*$qty,2,'.','').'</td><td><small><a href="remove.php?id='.urlencode(base64_encode($id)).'&item='.urlencode(base64_encode($key)).'">remove</a></small></td></tr>';
$counter++;
$totalprice+=$item['price']*$qty;
$totalqty+=$qty;
}
if ($alt)
{
$content .= '<tr class="alt" valign="top">';
$alt=false;
} else {
$content .= '<tr valign="top">';
$alt=true;
}
$content .= '<td align="right">&nbsp;</td><td><strong>Total</strong></td><td align="right">'.$totalqty.'</td><td align="right">&nbsp;</td><td align="right">$'.number_format($totalprice,2,'.','').'</td><td>&nbsp;</td></tr>';
$content .= '</table></td></tr></table>';
}
if ($totalqty==0)
{
$content = '<p>Your Bag Is Empty.</p>';
}
$layout=join('',file('layout.html'));
$html=str_replace('<!--Content-->',$content,$layout);
$html=str_replace('<!--id-->',urlencode(base64_encode($id)),$html);
echo $html;
?>

------------------------------------------------
remove.php
------------------------------------------------

<?php

//remove.php - remove item from cart

$id = base64_decode($_GET['id']);
$item = base64_decode($_GET['item']);
if (strlen($id)!=32)
{
$content = 'You do not belong here.';
} else {
$cart=unserialize(base64_decode(@join('',@file('carts/'.$id))));
$newcart=array();
foreach ($cart as $key=>$value)
{
if ($key!=$item)
{
$newcart[$key]=$value;
}
}
if (count($cart)>0)
{
$fp=fopen('carts/'.$id,'w');
fwrite($fp,base64_encode(serialize($newcart)));
fclose($fp);
} else {
unlink('carts/'.$id);
}
}
Header('Location: view.php?id='.base64_encode($id));
?>

Not 100% verified but seems to work ok at
<snip>

You can write the checkout, I'm beat.

"Kind Of" reminds me of doing Pascal homework for favours in col ;-) Good 'ole days i reckon.

[edited by: jatar_k at 4:26 pm (utc) on Aug. 14, 2003]
[edit reason] no urls thanks [/edit]

kelly_boyce

10:59 am on Aug 14, 2003 (gmt 0)

10+ Year Member



thanx alot for that waitman, you dont know how much i appreciate this. thanx.

there seems to be one problem with the code though. When i try the one at <snip> that works, but my one doesnt fully work. in my one, when i add the item to the cart it says its been added but then when i click on view it says bag is empty.

Any ideas? could it be coz i am using PHP4 on IIS? if so how can i solve the problem?

I will sort out the checkout bit my self somehow, but the main thing is for me to get the adding to cart working.

many thanx in advance.

kelly

[edited by: jatar_k at 4:27 pm (utc) on Aug. 14, 2003]
[edit reason] no urls thanks [/edit]

waitman

1:59 pm on Aug 14, 2003 (gmt 0)

10+ Year Member



you are welcome.

you need to make sure that your web server process has permission to write to the "carts" directory.

you may wish to move the "carts" directory somewhere outside of the web root to avoid a security issue.

take care,

waitman

3:55 pm on Aug 14, 2003 (gmt 0)

10+ Year Member



ok, this might work a little better. it attempts to use cookies and sessions (if available). should work ok without too. give it a try.

------------------------------------------
layout.html
------------------------------------------

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
<title>Cheap Chickie Cart By Waitman</title>
<style type="text/css">
body { font-family: verdana; font-size: 12px; color: #5c5c5c; }
td { font-size: 12px; }
.alt { background-color: #eeeeee; }
.alt td { border-bottom: 1px solid #cccccc; border-top: 1px solid #cccccc; }
.bone { border: 2px solid #cccccc; }
</style>
</head>

<body>
<h3>Cheaper Cart</h3>
<p></p><a href="index.php?id=<!--id-->">home</a> ¦ <a href="view.php?id=<!--id-->">view</a></p>
<!--Content-->

</body>
</html>

------------------------------------------
index.php
------------------------------------------

<?php

//index.php - show a product to add

session_start();

if (!session_is_registered('id')) {
session_register('id');
}

$id=base64_decode($_SESSION['id']);

if (strlen($id)!=32)
{
$id=base64_decode($_GET['id']);
}

if (strlen($id)!=32)
{
$id=md5(time().rand(99,9999999));
}

$id=base64_encode($id);
$price='43.32';
$sku='1553';
$checksum=md5($sku.$price.'xxxtopsecret');

$content = '<form method="post" action="add.php?id='.urlencode(base64_encode($id)).'"><input type="hidden" name="price" value="'.$price.'"><input type="hidden" name="checksum" value="'.$checksum.'"><input type="hidden" name="sku" value="'.$sku.'"><select name="style"><option value="snake skin">snake skin</option><option value="booty">booty</option><option value="right one">right one</option></select><select name="size"><option value="xsm">xsm</option><option value="sm">sm</option><option value="m">m</option><option value="xm">xm</option><option value="xxx">xxx</option></select><select name="color"><option value="green">green</option><option value="yellow">yellow</option><option value="orange">orange</option></select><select name="qty"><option value="1">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option><option value="5">5</option><option value="6">6</option></select><input type="submit" name="submit" value="add to cart"></form>';

$layout=join('',file('layout.html'));
$html=str_replace('<!--Content-->',$content,$layout);
$html=str_replace('<!--id-->',urlencode($id),$html);
echo $html;
?>

------------------------------------------
add.php
------------------------------------------

<?php

session_start();

$id=base64_decode($_SESSION['id']);
if (strlen($id)!=32)
{
$id=base64_decode($_GET['id']);
}
if (strlen($id)!=32)
{
$content = 'please quit messing with the cart id, sirmam.';
} else {
$cart=unserialize(base64_decode(@join('',@file('carts/'.$id))));
$ym=array();
$checksum=md5($_POST['sku'].$_POST['price'].'xxxtopsecret');
if ($checksum!= $_POST['checksum'])
{
$content = 'bad checksum. goobye';
} else {
$ym['sku']=$_POST['sku'];
$ym['price']=$_POST['price'];
$qty=intval($_POST['qty']);
$ym['style']=$_POST['style'];
$ym['size']=$_POST['size'];
$ym['color']=$_POST['color'];
$id=md5($_POST['sku'].$_POST['style'].$_POST['size'].$_POST['color']);
if ($qty>0)
{
$cart[$id]['qty']+=$qty;
$cart[$id]['item']=base64_encode(serialize($ym));
$fp=fopen('carts/'.$id,'w');
fwrite($fp,base64_encode(serialize($cart)));
fclose($fp);
$content = 'Item Added. Thank You.';
// {or} header('Location: view.php');
} else {
$content = "You didn't pick a thing to add.";
}
}
}
$layout=join('',file('layout.html'));
$id=base64_encode($id);
$html=str_replace('<!--Content-->',$content,$layout);
$html=str_replace('<!--id-->',urlencode($id),$html);
echo $html;
?>

------------------------------------------
view.php
------------------------------------------

<?php

//view.php - view your cart
session_start();

$id=base64_decode($_SESSION['id']);
if (strlen($id)!=32)
{
$id=base64_decode($_GET['id']);
}
if (strlen($id)!=32)
{
$content = 'Your bag is obviously empty.';
} else {
$cart=unserialize(base64_decode(@join('',@file('carts/'.$id))));
$content = '<p><strong>Your Bag.</strong></p><table border="0" cellspacing="0" cellpadding="10"><tr><td class="bone"><table border="0" cellspacing="0" cellpadding="10"><td align="right"><strong>#</strong></td><td><strong>Item</strong></td><td align="right"><strong>Qty</strong></td><td align="right"><strong>Unit</strong></td><td align="right"><strong>Amount</strong></td><td>&nbsp;</td></tr>';
$counter=1;
$alt=true;
$total=0.0;
if (is_array($cart))
{
foreach ($cart as $key=>$value)
{
$qty = $value['qty'];
$item = unserialize(base64_decode($value['item']));
if ($alt)
{
$content .= '<tr class="alt" valign="top">';
$alt=false;
} else {
$content .= '<tr valign="top">';
$alt=true;
}
$content .= '<td align="right">'.$counter.'</td><td><strong>'.$item['sku'].'</strong><br><small><em>style:</em> '.$item['style'].' <em>size:</em> '.$item['size'].' <em>color:</em> '.$item['color'].'</small></td><td align="right">'.$qty.'</td><td align="right">$'.number_format($item['price'],2,'.','').'</td><td align="right">$'.number_format($item['price']*$qty,2,'.','').'</td><td><small><a href="remove.php?id='.urlencode(base64_encode($id)).'&item='.urlencode(base64_encode($key)).'">remove</a></small></td></tr>';
$counter++;
$totalprice+=$item['price']*$qty;
$totalqty+=$qty;
}
if ($alt)
{
$content .= '<tr class="alt" valign="top">';
$alt=false;
} else {
$content .= '<tr valign="top">';
$alt=true;
}
$content .= '<td align="right">&nbsp;</td><td><strong>Total</strong></td><td align="right">'.$totalqty.'</td><td align="right">&nbsp;</td><td align="right">$'.number_format($totalprice,2,'.','').'</td><td>&nbsp;</td></tr>';
$content .= '</table></td></tr></table>';
}
}
if ($totalqty==0)
{
$content = '<p>Your Bag Is Empty.</p>';
}
$id=base64_encode($id);
$layout=join('',file('layout.html'));
$html=str_replace('<!--Content-->',$content,$layout);
$html=str_replace('<!--id-->',urlencode($id),$html);
echo $html;
?>

------------------------------------------
remove.php
------------------------------------------

<?php

//remove.php - remove item from cart
session_start();

$id=base64_decode($_SESSION['id']);
if (strlen($id)!=32)
{
$id=base64_decode($_GET['id']);
}
$item = base64_decode($_GET['item']);
if (strlen($id)!=32)
{
$content = 'You do not belong here.';
} else {
$cart=unserialize(base64_decode(@join('',@file('carts/'.$id))));
$newcart=array();
foreach ($cart as $key=>$value)
{
if ($key!=$item)
{
$newcart[$key]=$value;
}
}
if (count($cart)>0)
{
$fp=fopen('carts/'.$id,'w');
fwrite($fp,base64_encode(serialize($newcart)));
fclose($fp);
} else {
unlink('carts/'.$id);
}
}
$id=base64_encode($id);
Header('Location: view.php?id='.urlencode($id));
?>

available at <snip>

seems to work a little better...

[edited by: jatar_k at 4:28 pm (utc) on Aug. 14, 2003]
[edit reason] no urls thanks [/edit]

waitman

5:26 pm on Aug 14, 2003 (gmt 0)

10+ Year Member



ok urls banned, odd policy but what the hey... ;-)

here is a little better version, with product catalog. all finished up with this project, got stuff in the hopper to take care of... so we can call this "final version".

take care,

------------------------------------
layout.html
------------------------------------
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
<title>Cheap Chickie Cart By Waitman</title>
<style type="text/css">
body { font-family: verdana; font-size: 12px; color: #5c5c5c; }
td { font-size: 12px; }
.alt { background-color: #eeeeee; }
.alt td { border-bottom: 1px solid #cccccc; border-top: 1px solid #cccccc; }
.bone { border: 2px solid #cccccc; }
.navigation { border: 1px dashed #cccccc; background: #eeeeee; }
.navdiv { padding: 10px; }
.content { padding: 10px; }
</style>
</head>

<body>
<h3>Cheaper Cart</h3>
<p></p><a href="index.php?id=<!--id-->">home</a> ¦ <a href="view.php?id=<!--id-->">view</a></p>
<table border="0" width="100%" cellspacing="1" cellpadding="1">
<tr valign="top">
<td width="175" height="350" class="navigation">
<!--Navigation-->
</td>
<td class="content" height="350">
<!--Content-->
</td>
</tr>
</table>
</body>
</html>

------------------------------------
index.php
------------------------------------

<?php

//index.php - show a product to add

session_start();

if (!session_is_registered('id')) {
session_register('id');
}

$id=base64_decode($_SESSION['id']);

if (strlen($id)!=32)
{
$id=base64_decode($_GET['id']);
}

if (strlen($id)!=32)
{
$id=md5(time().rand(99,9999999));
}

$id=base64_encode($id);
@include_once('products.php');
$didshow=false;

if (strlen($_GET['showcategory'])<1)
{
$content = '<h3>Welcome to the store.</h3><p>Please browse our latest selection using the navigational links along the left side of the page.</p>';
} else {
$content = '<h3>Super Deals on Hot Items!</h3><p>Here are the available items in our '.htmlspecialchars($_GET['showcategory']).' category.</p><form method="post" action="add.php?id='.urlencode($id).'"><table border="0" cellspacing="0" cellpadding="5">';
foreach ($products as $key=>$value)
{

if ($value['category']==$_GET['showcategory'])
{
$didshow=true;
$styleoptions = '';
$styles = @explode(',',$value['styles']);
foreach ($styles as $option)
{
$styleoptions .= '<option value="'.$option.'">'.$option.'</option>';
}

$coloroptions = '';
$colors = @explode(',',$value['colors']);
foreach ($colors as $option)
{
$coloroptions .= '<option value="'.$option.'">'.$option.'</option>';
}

$sizeoptions = '';
$sizes = @explode(',',$value['sizes']);
foreach ($sizes as $option)
{
$sizeoptions .= '<option value="'.$option.'">'.$option.'</option>';
}

$price=$value['price'];
$sku=$key;
$checksum=md5($sku.$price.'xxxtopsecret');

$content .= '
<tr valign="top"><td colspan="4"><strong>'.$sku.'</strong></td></tr><tr valign="top"><td><input type="hidden" name="price['.$sku.']" value="'.$price.'"><input type="hidden" name="checksum['.$sku.']" value="'.$checksum.'"><input type="hidden" name="sku['.$sku.']" value="'.$sku.'"><select name="style['.$sku.']">'.$styleoptions.'</select></td><td><select name="color['.$sku.']">'.$coloroptions.'</select></td><td><select name="size['.$sku.']">'.$sizeoptions.'</select></td><td><select name="qty['.$sku.']"><option value="0">--</option><option value="1">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option><option value="5">5</option><option value="6">6</option></select></td></tr>';
}
}
$content .= '</table><input type="submit" name="submit" value="add to cart"></form>';
}
if (!$didshow)
{
$content = "<h3>I'm Sorry!</h3><p>We do not have any items available in that category, please check again later.</p>";
}
$layout=join('',file('layout.html'));
$html=str_replace('<!--Content-->',$content,$layout);
$html=str_replace('<!--id-->',urlencode($id),$html);
$html=str_replace('<!--Navigation-->',$navigation,$html);
echo $html;
?>

------------------------------------
add.php
------------------------------------

<?php

session_start();
$content = '';
$tqty=0;
$id=base64_decode($_SESSION['id']);
if (strlen($id)!=32)
{
$id=base64_decode($_GET['id']);
}
if (strlen($id)!=32)
{
$content = 'please quit messing with the cart id, sirmam.';
} else {
$cart=unserialize(base64_decode(@join('',@file('carts/'.$id))));
$ym=array();

if (!is_array($_POST['sku']))
{
$content='you did not select anything to add to your cart.';
} else {

foreach ($_POST['sku'] as $val)
{
$checksum=md5($val.$_POST['price'][$val].'xxxtopsecret');
if ($checksum!= $_POST['checksum'][$val])
{
$content .= 'bad checksum. goobye <br>';
} else {
$ym['sku']=$val;
$ym['price']=$_POST['price'][$val];
$qty=intval($_POST['qty'][$val]);
$ym['style']=$_POST['style'][$val];
$ym['size']=$_POST['size'][$val];
$ym['color']=$_POST['color'][$val];
$pid=md5($val.$ym['style'].$ym['size'].$ym['color']);
if ($qty>0)
{
$cart[$pid]['qty']+=$qty;
$cart[$pid]['item']=base64_encode(serialize($ym));
$content .= 'Item '.htmlspecialchars($val).' Added. Thank You.<br>';
$tqty+=$qty;
}
}
}
}
if ($tqty<1)
{
$content = "You didn't pick a thing to add.";
} else {
$fp=fopen('carts/'.$id,'w');
fwrite($fp,base64_encode(serialize($cart)));
fclose($fp);
}
}
$layout=join('',file('layout.html'));
$id=base64_encode($id);
@include_once('products.php');
$html=str_replace('<!--Content-->',$content,$layout);
$html=str_replace('<!--id-->',urlencode($id),$html);
$html=str_replace('<!--Navigation-->',$navigation,$html);
echo $html;
?>

------------------------------------
view.php
------------------------------------

<?php

//view.php - view your cart
session_start();

$id=base64_decode($_SESSION['id']);
if (strlen($id)!=32)
{
$id=base64_decode($_GET['id']);
}
if (strlen($id)!=32)
{
$content = 'Your bag is obviously empty.';
} else {
$cart=unserialize(base64_decode(@join('',@file('carts/'.$id))));
$content = '<p><strong>Your Bag.</strong></p><table border="0" cellspacing="0" cellpadding="10"><tr><td class="bone"><table border="0" cellspacing="0" cellpadding="10"><td align="right"><strong>#</strong></td><td><strong>Item</strong></td><td align="right"><strong>Qty</strong></td><td align="right"><strong>Unit</strong></td><td align="right"><strong>Amount</strong></td><td>&nbsp;</td></tr>';
$counter=1;
$alt=true;
$total=0.0;
if (is_array($cart))
{
foreach ($cart as $key=>$value)
{
$qty = $value['qty'];
$item = unserialize(base64_decode($value['item']));
if ($alt)
{
$content .= '<tr class="alt" valign="top">';
$alt=false;
} else {
$content .= '<tr valign="top">';
$alt=true;
}
$content .= '<td align="right">'.$counter.'</td><td><strong>'.$item['sku'].'</strong><br><small><em>style:</em> '.$item['style'].' <em>size:</em> '.$item['size'].' <em>color:</em> '.$item['color'].'</small></td><td align="right">'.$qty.'</td><td align="right">$'.number_format($item['price'],2,'.','').'</td><td align="right">$'.number_format($item['price']*$qty,2,'.','').'</td><td><small><a href="remove.php?id='.urlencode(base64_encode($id)).'&item='.urlencode(base64_encode($key)).'">remove</a></small></td></tr>';
$counter++;
$totalprice+=$item['price']*$qty;
$totalqty+=$qty;
}
if ($alt)
{
$content .= '<tr class="alt" valign="top">';
$alt=false;
} else {
$content .= '<tr valign="top">';
$alt=true;
}
$content .= '<td align="right">&nbsp;</td><td><strong>Total</strong></td><td align="right">'.$totalqty.'</td><td align="right">&nbsp;</td><td align="right">$'.number_format($totalprice,2,'.','').'</td><td>&nbsp;</td></tr>';
$content .= '</table></td></tr></table>';
}
}
if ($totalqty==0)
{
$content = '<p>Your Bag Is Empty.</p>';
}
$id=base64_encode($id);
@include_once('products.php');
$layout=join('',file('layout.html'));
$html=str_replace('<!--Content-->',$content,$layout);
$html=str_replace('<!--id-->',urlencode($id),$html);
$html=str_replace('<!--Navigation-->',$navigation,$html);echo $html;
?>

------------------------------------
remove.php
------------------------------------

<?php

//remove.php - remove item from cart
session_start();

$id=base64_decode($_SESSION['id']);
if (strlen($id)!=32)
{
$id=base64_decode($_GET['id']);
}
$item = base64_decode($_GET['item']);
if (strlen($id)!=32)
{
$content = 'You do not belong here.';
} else {
$cart=unserialize(base64_decode(@join('',@file('carts/'.$id))));
$newcart=array();
foreach ($cart as $key=>$value)
{
if ($key!=$item)
{
$newcart[$key]=$value;
}
}
if (count($cart)>0)
{
$fp=fopen('carts/'.$id,'w');
fwrite($fp,base64_encode(serialize($newcart)));
fclose($fp);
} else {
unlink('carts/'.$id);
}
}
$id=base64_encode($id);
Header('Location: view.php?id='.urlencode($id));
?>

------------------------------------
products.php
------------------------------------

<?php

//products.php - build product database

$categories=array();
$categories[]='smart deals';
$categories[]='balloons';
$categories[]='monster trucks';
$categories[]='mini vessels';

$products=array();

$products['1553']['category']='smart deals';
$products['1553']['styles']='fancy,regular,extra regular,boring';
$products['1553']['colors']='red,green,blue,yeller';
$products['1553']['sizes']='xsm,sm,m,xm,l,xl';
$products['1553']['price']='43.25';

$products['1492']['category']='smart deals';
$products['1492']['styles']='fancy,regular,extra regular,boring';
$products['1492']['colors']='red,green,blue,yeller,pink';
$products['1492']['sizes']='xsm,sm,m,xm,l,xl,2xl';
$products['1492']['price']='19.95';

$products['4563-topper']['category']='smart deals';
$products['4563-topper']['styles']='fancy,regular,extra regular,boring';
$products['4563-topper']['colors']='red,green,blue,yeller,oranga duranga';
$products['4563-topper']['sizes']='xsm,sm,m,xm,l,xl';
$products['4563-topper']['price']='69.69';

$products['6623']['category']='monster trucks';
$products['6623']['styles']='fancy,regular,extra regular,boring';
$products['6623']['colors']='red,green,blue,yeller';
$products['6623']['sizes']='xsm,sm,m,xm,l,xl';
$products['6623']['price']='43.25';

$products['7874-race']['category']='monster trucks';
$products['7874-race']['styles']='fancy,regular,extra regular,boring';
$products['7874-race']['colors']='red,green,blue,yeller,pink';
$products['7874-race']['sizes']='xsm,sm,m,xm,l,xl,2xl';
$products['7874-race']['price']='19.95';

$products['chop52']['category']='mini vessels';
$products['chop52']['styles']='fancy,regular,extra regular,boring';
$products['chop52']['colors']='red,green,blue,yeller,oranga duranga';
$products['chop52']['sizes']='xsm,sm,m,xm,l,xl';
$products['chop52']['price']='69.69';

$navigation = '';
foreach ($categories as $category)
{
$navigation .= '<div class="navdiv"><a href="index.php?id='.urlencode($id).'&showcategory='.urlencode($category).'">'.htmlspecialchars($category).'</a></div>';
}
?>

waitman

5:34 pm on Aug 14, 2003 (gmt 0)

10+ Year Member



overlooked bug in the first and second version of the cart, fixed in the third version. calculating "sku id" to add item to cart is messing with the session id.

$id=md5($_POST['sku'].$_POST['style'].$_POST['size'].$_POST['color']);
if ($qty>0)
{
$cart[$id]['qty']+=$qty;
$cart[$id]['item']=base64_encode(serialize($ym));

should be

$pid=md5($_POST['sku'].$_POST['style'].$_POST['size'].$_POST['color']);
if ($qty>0)
{
$cart[$pid]['qty']+=$qty;
$cart[$pid]['item']=base64_encode(serialize($ym));

waitman

5:39 pm on Aug 14, 2003 (gmt 0)

10+ Year Member



one more little bug. third version of cart with product db:

index.php

if (strlen($_GET['showcategory'])<1)
{
$content = '<h3>Welcome to the store.</h3><p>Please browse our latest selection using the navigational links along the left side of the page.</p>';

should be

if (strlen($_GET['showcategory'])<1)
{
$didshow=true;
$content = '<h3>Welcome to the store.</h3><p>Please browse our latest selection using the navigational links along the left side of the page.</p>';