Aims:
session_start
function
session_start
must come before any (X)HTML is generated.
Why?
session_start
do? Continued...$_SESSION
$_SESSION
array, or put new data into the array
counter.php
: simple exampleCounting 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
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 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) );
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>