PHP: Sessions

Derek Bridge

Department of Computer Science,
University College Cork

PHP: Sessions

Aims:

The full shopping cart system: cart.zip
(Remember to edit $host, $user, $password and $dbname)

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 
    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

Sessions in PHP

Session in PHP: advanced notes

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

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

Overview of the scripts

[The scripts used in our shopping cart system]

Displaying our wine 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 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