CS 199 Willamette University Spring, 2019
 
Programming in PHP, Databases with MySQL,
and Web Applications
 

Laboratory Exercise on Session Variables

Summary: This laboratory exercise builds on the previous lab on cookies. In this lab, the use of cookies is expanded to a different type of variable, called a session variable.

Session Variables

Effectively, session variables are cookies that remain active only while the browser is actively interacting with the server. When time elapses, or when you close your browser, the session variables disappear. (If cookies are not allowed by a user, then information for sessions may be placed in a query string at the end of a URL.)

This lab illustrates a type of application, combining session variables with php, SQL, and MySQL.

Quick Review

The following lines from session-cookies-2.php illustrate typically processing of session variables.


// check if person has logged in previously
session_start();
$processingOK = "not yet";
$firstLogin = "no";
if (isset ($_SESSION['authorized'])) {
  // user already logged in
  $processingOK = $_SESSION['authorized'];
} else {
  // user not logged in, so check password
  $password = trim($_POST['password']);
  if ($password == 'Test') {
    // correct password given
    $processingOK = 'ok';
    $_SESSION['authorized'] = 'ok';
    $firstLogin="yes";
  } else {
    // invalid password
  }
}

The following notes provide additional review.

Steps for this Lab

    In past labs, we have worked with a groceryOrders table. One version of the table begins as follows:

    +-------------+-------------+--------+----------+
    | orderItemID | orderNumber | itemID | quantity |
    +-------------+-------------+--------+----------+
    |           1 |           1 |      1 |     5.10 |
    |           2 |           1 |      5 |     3.70 |
    |           3 |           1 |     11 |     2.50 |
    |           4 |           2 |      1 |     3.50 |
    |           5 |           2 |      5 |     4.50 |
    |           6 |           2 |      9 |     4.20 |
    |           7 |           2 |     13 |     5.10 |
    |           8 |           2 |     17 |     0.50 |
    |           9 |           2 |     22 |     2.60 |
    |          10 |           3 |      1 |     2.60 |
    . . .
    

    In this example, order 1 contains three items, and order 2 contains 6 items.

  1. Create a starting html form that asks a user for an order number.

  2. Upon clicking submit in the order-number for, create a response page that sets a session variable to remember the order number. The response page then should join the groceryOrder table with the groceries table, to display a listing of the grocery items for the order, including all item information from the groceries table, together with the quantity desired for each item.
    Hint: Join the two tables, looking only at those items from the specified order number.

  3. Building on the previous step and also on the MySQL/Web project, expand the response page to include all grocery items to include an input box for the quantity of each item ordered. When the original order included a quantity, that amount should be displayed (subject to user editing), and when the origianl order did not include a quantity, the order box should be left blank.

    In summary, this order form allows a user to modify the order, as desired!

  4. Expand the MySQL/Web project to update the groceryOrders table.

    1. No need to create a new table—the groceryOrders table already exists!
    2. Delete all previous order items for the given order (use the session variable to identify which order is involved.
    3. For each item ordered, insert a new record for the given order number—adjusting the last part of the MySQL/Web project.



created 25 April 2019
revised 2 May 2019
Valid HTML 4.01! Valid CSS!