<?php
//add.php?item=&price=&quantity=

//Have all required fields been specified?
if (empty($_POST['item']) || empty($_POST['price']) || empty($_POST['quantity'])) {
    
//Not all elements have been specified!
    //Redirect users to buy.php (Location needs to be an absolute URI)
    
header('Location: http://'.$_SERVER["HTTP_HOST"].dirname($_SERVER["SCRIPT_NAME"]).'/buy.php');
    exit;
}

//Start the session
session_start();
?>
<!DOCTYPE html 
     PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>Add an item</title>
</head>
<body>
<?php
//Read in variables from $_POST
$price     $_POST['price'];
$item      $_POST['item'];
$quantity  $_POST['quantity'];

echo 
'<h1>Add to cart</h1>';

echo 
"<p>Thank you for wanting a <strong>$item</strong>!</p>";

//We define an associative array with the details of our new item
$cart_row = array(
    
'item' => $item,
    
'unitprice' => $price,
    
'quantity' => $quantity
);

//If $_SESSION['cart'] hasn't yet been defined, define it as an array
if (!isset($_SESSION['cart']))
    
$_SESSION['cart'] = array();

//Append the item to the $_SESSION['cart'] session variable
// ($array[] means "append a new item to the end of $array")
$_SESSION['cart'][] = $cart_row;

var_dump($_SESSION);

echo 
"<p>Your item has been added to the cart.</p>";

echo 
"<a href=\"cart.php\">Go to cart!</a>";

?>
</body>
</html>