Aims:
The full shopping cart system: cart.zip
(Remember to edit $host, $user,
$password and $dbname)
session_start function
session_start do? Continued…$_SESSION
$_SESSION
array, or put new data into the array
session_start before any HTML is generated
Why?
counter.php: simple exampleCounting the number of times a page has been accessed by a client
<?php
require_once( 'output_functions.php' );
session_start();
if ( ! isset($_SESSION['num_visits']) )
{
$_SESSION['num_visits'] = 1;
}
else
{
$_SESSION['num_visits']++;
}
output_header( 'Welcome!', 'stylesheet.css' );
output_paragraph( "You have visited this page {$_SESSION['num_visits']} times" );
output_footer( 'University College Cork' );
?>
Compare this with our previous version! Compare (a) the code, (b) the behaviour, and (c) what gets stored where
session_register to explicitly register a variablesession_unregister to remove a variable from the data storesession_is_registered to determine whether a variable is registeredsession_id if you want to see the unique session IDsession_destroy: removes all data from this session's data
store (it doesn't affect the cookie, which is client-side)
Suppose our company Shrine of Bacchus Wines sell wines, listed in this database:
CREATE TABLE wines
(
id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(255) NOT NULL,
price DECIMAL(5, 2) NOT NULL,
PRIMARY KEY (id)
);
The product catalog page lists all the wines we sell:
<table>
<tr>
<td>Dingo Dribble</td>
<td>12.33</td>
<td><a href="add_to_cart.php?id=1">Add to cart</a></td>
</tr>
…
</table>
(Obviously, this table is produced from the database by a PHP script:
show_catalog.php)
This page also contains a link:
<a href="show_cart.php">Show cart</a>
add_to_cart.php: adding to the shopping cart
<?php
require_once('output_functions.php');
// If this is an on-going session, obtain the $cart array.
// If it's a new session, create an empty array
session_start();
if ( isset($_SESSION['cart']) )
{
$cart = $_SESSION['cart'];
}
else
{
$cart = array();
}
// Get the id of the item being added to the cart
$id = (int) $_GET['id'];
// If this item is in the cart already, then increment the quantity.
// If not, insert it.
if ( isset($cart[$id]) )
{
$cart[$id]++;
}
else
{
$cart[$id] = 1;
}
// Put the revised cart back into the session store
$_SESSION['cart'] = $cart;
output_header( 'Item successfully added to your cart', 'sbw.css' );
?>
<p>
<a href="show_cart.php">Show cart</a>
</p>
<p>
<a href="show_catalog.php">Show catalog</a>
</p>
<?php
output_footer( 'Shrine of Bacchus Wines' );
?>
show_cart.php: displaying the shopping cart (simple version)
<?php
require_once( 'output_functions.php' );
// If this is an on-going session, obtain the $cart array.
// If it's a new session, create an empty array
session_start();
if ( isset($_SESSION['cart']) )
{
$cart = $_SESSION['cart'];
}
else
{
$cart = array();
}
output_header( 'Your cart', 'sbw.css' );
if ( count($cart) == 0 )
{
output_paragraph( 'No items in shopping cart.' );
}
else
{
echo "<table>";
foreach ($cart as $id => $qty)
{
echo "<tr>
<td>{$id}</td>
<td>{$qty}</td>
</tr>";
}
echo "</table>";
}
?>
<p>
<a href="show_catalog.php">Show catalog</a>
</p>
<?php
output_footer( 'Shrine of Bacchus Wines' );
?>
show_cart.php: displaying the shopping cart (better version)
A better version of show_cart.php would take each product in the cart and
look it up in the database so that it can output not just the id and quantity
but also the name and price