Sessions

Derek Bridge

Department of Computer Science,
University College Cork

Sessions

Aims:

Sessions

Sessions in PHP

Sessions in PHP

Sessions in PHP

counter.php: simple example

Counting the number of times a page has been accessed by a client

<?php 
 session_start();
 if (! isset($_SESSION['numvisits'])) 
 {
    $_SESSION['numvisits'] = 1;
 }
 else 
 {
    $_SESSION['numvisits]++;
 }
?>
...
<?php
 echo "Welcome! (Visit number: {$_SESSION['numvisits']})";
?>

Compare this with our previous version! Compare (a) the code, (b) the behaviour, and (c) what gets stored where

Sessions in PHP

Session in PHP: advanced notes

Case study: a very, very, very simple shopping cart

Suppose we 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)
);

Displaying our product catalog

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 should be 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
 // 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 = $_GET['id'];
 
 // If this item is in the cart already, then increment the quantity.
 // If not, insert it.
 if ( array_key_exists($id, $cart) )
 {
	$cart[$id]++;
 }
 else
 {
	$cart[$id] = 1;
 }
 
 // Put the revised cart back into the session store
 $_SESSION['cart'] = $cart;
?>

<?php echo '<?xml version="1.0" encoding="UTF-8"?>'; ?>
<!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" lang="en">
<head>
  <title>Item Successfully Added to Cart</title>
</head>

<body>

<p>Item successfully added to cart</p>
<p><a href="show_cart.php">Show cart</a></p>
<p><a href="show_catalog.php">Show catalog</a></p>

</body>
</html>

show_cart.php: displaying the shopping cart

<?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();
 }
?>

<?php echo '<?xml version="1.0" encoding="UTF-8"?>'; ?>
<!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" lang="en">
<head>
  <title>Your shopping cart</title>
</head>

<body>

<?php
 if ( count($cart) == 0 )
 {
	echo 'No items in shopping cart.';
 }
 else
 {
	include('dbconnect.php');
	$total = 0;
	echo '<table>';
	foreach ($cart as $id => $qty)
	{
		// Get details of the wine whose $id is in the cart
		$sql = "SELECT * FROM wines WHERE id = {$id};";
		$dbresult = mysql_query($sql);
		if ( ! $dbresult )
		{
			die('Error in query ' . mysql_error());
		}
		$row = mysql_fetch_assoc($dbresult);
		echo "<tr>
				<td>{$row['name']}</td>
				<td>{$row['price']}</td>
				<td>{$qty}</td>
			</tr>";
		$total += $row['price'] * $qty;
	}
	echo "<tr><th>TOTAL:</th><td>{$total}</td></tr>";
	echo "</table>";
 }
?>

<p><a href="show_catalog.php">Show catalog</a></p>

</body>
</html>