In response to these changes, we have updated the source code for our book. Detailed changes for each chapter are listed below; most changes are as the direct result of variable access changes or because of short tag requirements. In addition, there are selected bug fixes in response to reader feedback. Please contact Hugh Williams at hugh @ hughwilliams.com with any suggestions or corrections.
$page_url = clean($page_url, 30);to
$page_url = clean($_GET["page_url"], 30);
include "include.inc"; $page_url = clean($page_url, 30);to
include "../db.inc"; $page_url = clean($_GET["page_url"], 30);
echo "RegionName is " . $regionName . "\n";to
echo "RegionName is " . $_GET["regionName"] . "\n";
$regionName = clean($regionName, 30); $wineType = clean($wineType, 10);to
$regionName = clean($_GET["regionName"], 30); $wineType = clean($_GET["wineType"], 10);
$regionName = clean($regionName, 30);to
$regionName = clean($_GET["regionName"], 30);
if (empty($regionName))to
if (empty($_GET["regionName"]))and
} // end of if empty($regionName) bodyto
} // end of if empty($_GET["regionName"]) bodyand
$regionName = clean($regionName, 30);to
$regionName = clean($_GET["regionName"], 30);
if (!empty($wineId) && !empty($qty))to
if (!empty($_GET["wineId"]) && !empty($_GET["qty"]))
// and the PHP environment variable $HTTP_REFERER header("Location: $HTTP_REFERER");to
// and the PHP server environment variable $_SERVER["HTTP_REFERER"] header("Location: {$_SERVER["HTTP_REFERER"]}");
$offset = clean($_GET["offset"], 6);and changing:
"&" . $browseStriang .to
"&" . $browseString .
$regionName = clean($regionName, 30);to
$regionName = clean($_GET["regionName"], 30);
$offset = clean($_GET["offset"], 6);
$regionName = clean($_GET["regionName"], 30); $description = clean($_GET["description"], 2048);
if (empty($regionName) || empty($description))to
if (empty($_GET["regionName"]) || empty($_GET["description"]))
$custID = clean($_GET["custID"], 4);
if (empty($regionName) || empty($description))to
if (empty($_POST["regionName"]) || empty($_POST["description"]))and
$regionName = clean($regionName, 50); $description = clean($description, 2048);to
$regionName = clean($_POST["regionName"], 50); $description = clean($_POST["description"], 2048);and
if (is_uploaded_file($userfile))to
if (is_uploaded_file($_FILES["userfile"]))and
$file = fopen($userfile, "r");to
$file = fopen($_FILES["userfile"], "r");and
fread($file, filesize($userfile));to
fread($file, filesize($_FILES["userfile"]));
$regionId = clean($regionId, 3); $status = clean($status, 1);to
$regionId = clean($_GET["regionId"], 3); $status = clean($_GET["status"], 1);
$region_id = clean($region_id, 4);to
$region_id = clean($_GET["region_id"], 4);
foreach($HTTP_POST_VARS as $varname => $value)to
foreach($_POST as $varname => $value)
$custID = clean($custID, 5);to
$custID = clean($_GET["custID"], 5);
$custID = clean($custID, 5);to
$custID = clean($_POST["custID"], 5);and
foreach($HTTP_POST_VARS as $varname => $value)to
foreach($_POST as $varname => $value)
if (empty($regionId))to
if (empty($_POST["regionId"]))and
$regionId = clean($regionId, 3); $regionName = clean($regionName, 20); $description = clean($description, 255);to
$regionId = clean($_POST["regionId"], 3); $regionName = clean($_POST["regionName"], 30); $description = clean($_POST["description"], 2048);
$custID = clean($custID, 5);to
$custID = clean($_GET["custID"], 5);
printf("%s", $HTTP_USER_AGENT);to
printf("%s", $_SERVER["HTTP_USER_AGENT"]);
if(!isset($count)) {to
if(!isset($_COOKIE["count"])) {Added:
$_COOKIE["start"] = $start;and changed:
$count++;to
$count = $_COOKIE["count"] + 1;and
$duration = time() - $start;to
$duration = time() - $_COOKIE["start"];
$count = 0; $start = time(); } else { $count++;to
$_SESSION["count"] = 0; $_SESSION["start"] = time(); } else { $_SESSION["count"]++;and
$duration = time() - $start;to
$duration = time() - $_SESSION["start"];
foreach($HTTP_POST_VARS as $varname => $value)to
foreach($_POST as $varname => $value)
$_SESSION["formVars"] = $formVars;and
// Is CustID a session variable? If so, initialise if (session_is_registered("custID")) $custID = $_SESSION["custID"];and
// Store the errors in the session variable $_SESSION["errors"] = $errors;
if (!empty($HTTP_GET_VARS["custID"])) $custID = clean($HTTP_GET_VARS["custID"], 5);to
// Retrieve the custID from the session variable (if set) if (session_is_registered("custID")) $custID = $_SESSION["custID"]; if (!empty($_GET["custID"])) $custID = clean($_GET["custID"], 5); // Initialise $formVars from the $_SESSION["formVars"] (if set) if (session_is_registered("formVars")) { $row = $_SESSION["formVars"]; $formVars["surname"] = $row["surname"]; $formVars["firstName"] = $row["firstName"]; $formVars["address1"] = $row["address1"]; $formVars["city"] = $row["city"]; $formVars["email"] = $row["email"]; $formVars["dob"] = $row["dob"]; }
if (!empty($custID) && empty($errors))to
if (!empty($custID) && empty($_SESSION["errors"]))
$_SESSION["custID"] = $custID;
$formVars["dob"] = $row["birth_date"]; $formVars["dob"] = substr($formVars["dob"], 8, 2) . "/" . substr($formVars["dob"], 5, 2) . "/" . substr($formVars["dob"], 0, 4);to
$formVars["dob"] = substr($row["birth_date"], 8, 2) . "/" . substr($row["birth_date"], 5, 2) . "/" . substr($row["birth_date"], 0, 4);
<h2>Hi there <?=$PHP_AUTH_USER?></h2>to
<h2>Hi there <?php echo $_SERVER["PHP_AUTH_USER"]; ?></h2>and
'<?=$PHP_AUTH_PW?>'!to
'<?php echo $_SERVER["PHP_AUTH_PW"]; ?>'!
if(!authenticated($PHP_AUTH_USER, $PHP_AUTH_PW))to
if(!authenticated($_SERVER["PHP_AUTH_USER"], $_SERVER["PHP_AUTH_PW"]))
if(strncmp("141.190.17", $REMOTE_ADDR, 10) != 0)to
if(strncmp("141.190.17", $_SERVER["REMOTE_ADDR"], 10) != 0)
if (isset($HTTP_SESSION_VARS["authenticatedUser"]))to
if (isset($_SESSION["authenticatedUser"]))and
logged_on_page($HTTP_SESSION_VARS["authenticatedUser"]);to
logged_on_page($_SESSION["authenticatedUser"]);and
login_page($HTTP_SESSION_VARS["loginMessage"]);to
login_page($_SESSION["loginMessage"]);
$appUsername = clean($HTTP_POST_VARS["formUsername"], 10); $appPassword = clean($HTTP_POST_VARS["formPassword"], 15);to
$appUsername = clean($_POST["formUsername"], 50); $appPassword = clean($_POST["formPassword"], 10);and
$authenticatedUser = $appUsername;to
$_SESSION["authenticatedUser"] = $appUsername;and
$loginIpAddress = $REMOTE_ADDR;to
$_SESSION["loginIpAddress"] = $_SERVER["REMOTE_ADDR"];and
$loginMessage =to
$_SESSION["loginMessage"] =
$appUsername = $HTTP_SESSION_VARS["authenticatedUser"]; $loginMessage = "User \"$appUsername\" has logged out"; session_register("loginMessage");to
$appUsername = $_SESSION["authenticatedUser"]; session_register("loginMessage"); $_SESSION["loginMessage"] = "User \"$appUsername\" has logged out";
// Set a boolean flag to check if // a user has authenticated $notAuthenticated = !isset($HTTP_SESSION_VARS["authenticatedUser"]); // Set a boolean flag to true if this request // originated from the same IP address // as the one that created this session $notLoginIp = isset($HTTP_SESSION_VARS["loginIpAddress"]) && ($HTTP_SESSION_VARS["loginIpAddress"] != $REMOTE_ADDR); // Check that the two flags are false if($notAuthenticated) { // The request does not identify a session session_register("loginMessage"); $loginMessage = "You have not been authorized to access the " . "URL $REQUEST_URI"; // Re-locate back to the Login page header("Location: " . $loginScript); exit; } else if($notLoginIp) { // The request did not originate from the machine // that was used to create the session. // THIS IS POSSIBLY A SESSION HIJACK ATTEMPT session_register("loginMessage"); $loginMessage = "You have not been authorized to access the " . "URL $REQUEST_URI from the address $REMOTE_ADDR";to
// Set a boolean flag to check if // a user has authenticated $notAuthenticated = !isset($_SESSION["authenticatedUser"]); // Set a boolean flag to true if this request // originated from the same IP address // as the one that created this session $notLoginIp = isset($_SESSION["loginIpAddress"]) && ($_SESSION["loginIpAddress"] != $_SERVER["REMOTE_ADDR"]); // Check that the two flags are false if($notAuthenticated) { // The request does not identify a session session_register("loginMessage"); $_SESSION["loginMessage"] = "You have not been authorized to access the " . "URL {$_SERVER["REQUEST_URI"]}"; // Re-locate back to the Login page header("Location: " . $loginScript); exit; } else if($notLoginIp) { // The request did not originate from the machine // that was used to create the session. // THIS IS POSSIBLY A SESSION HIJACK ATTEMPT session_register("loginMessage"); $_SESSION["loginMessage"] = "You have not been authorized to access the " . "URL {$_SERVER["REQUEST_URI"]} from the address {$_SERVER["REMOTE_ADDR"]}";1c1
foreach($HTTP_POST_VARS as $varname => $value)to
foreach($_POST as $varname => $value)Added:
$_SESSION["formVars"] = $formVars;Added:
// Store the errors in the session variable $_SESSION["errors"] = $errors;Changed:
$loginUsername = $formVars["email"];to
$_SESSION["loginUsername"] = $formVars["email"];and
$custID = getCustomerID($loginUsername, $connection);to
$custID = getCustomerID($_SESSION["loginUsername"], $connection);
// Initialise $formVars from the $_SESSION["formVars"] (if set) if (session_is_registered("formVars")) { $row = $_SESSION["formVars"]; $formVars["title"] = $row["title"]; $formVars["surname"] = $row["surname"]; $formVars["firstName"] = $row["firstName"]; $formVars["initial"] = $row["initial"]; $formVars["address1"] = $row["address1"]; $formVars["address2"] = $row["address2"]; $formVars["address3"] = $row["address3"]; $formVars["city"] = $row["city"]; $formVars["state"] = $row["state"]; $formVars["zipcode"] = $row["zipcode"]; $formVars["country"] = $row["country"]; $formVars["phone"] = $row["phone"]; $formVars["fax"] = $row["fax"]; $formVars["email"] = $row["email"]; $formVars["dob"] = $row["dob"]; }Changed:
if (session_is_registered("loginUsername") && empty($errors))to
if (session_is_registered("loginUsername") && empty($_SESSION["errors"]))and
$custID = getCustomerID($loginUsername, $connection);to
$custID = getCustomerID($_SESSION["loginUsername"], $connection);Deleted:
// Reset the errors $errors = array();Neatened:
$formVars["dob"] = $row["birth_date"]; $formVars["dob"] = substr($formVars["dob"], 8, 2) . "/" . substr($formVars["dob"], 5, 2) . "/" . substr($formVars["dob"], 0, 4);to
$formVars["dob"] = substr($row["birth_date"], 8, 2) . "/" . substr($row["birth_date"], 5, 2) . "/" . substr($row["birth_date"], 0, 4);Replaced all references in the HTML <form> to $errors with $_SESSION["errors"]
$message = "You must login to view your customer receipt.";to
$_SESSION["message"] = "You must login to view your customer receipt.";and
if (!isset($custID))to
if (!isset($_GET["custID"]))and
$message = "Incorrect parameters to example.customer.3.php";to
$_SESSION["message"] = "Incorrect parameters to example.customer.3.php";and
header("Location: $HTTP_REFERER");to
header("Location: {$_SERVER["HTTP_REFERER"]}");Added:
// Initialise the $custID from the GET method request $custID = $_GET["custID"];and changed:
// Check this customer matches the custID if ($custID != getCustomerID($loginUsername, NULL)) { session_register("message"); $message = "You can only view your own customer receipt!"; $custID = getCustomerID($loginUsername, NULL); }to
// Check this customer matches the custID if ($custID != getCustomerID($_SESSION["loginUsername"], NULL)) { session_register("message"); $SESSION["message"] = "You can only view your own customer receipt!"; $custID = getCustomerID($_SESSION["loginUsername"], NULL); }
$_SESSION["loginUsername"] = $loginUsername;Changed:
$message = "Username or password incorrect. Login failed.";to
$_SESSION["message"] = "Username or password incorrect. Login failed.";Removed:
global $message;Changed:
if (isset($HTTP_POST_VARS["loginUsername"])) $loginUsername = clean($HTTP_POST_VARS["loginUsername"], 30);to
if (isset($_POST["loginUsername"])) $loginUsername = clean($_POST["loginUsername"], 30);and
if (isset($HTTP_POST_VARS["loginPassword"])) $loginPassword = clean($HTTP_POST_VARS["loginPassword"], 8);to
if (isset($_POST["loginPassword"])) $loginPassword = clean($_POST["loginPassword"], 8);and
if ((empty($HTTP_POST_VARS["loginUsername"]) && !empty($HTTP_POST_VARS["loginPassword"])) || (!empty($HTTP_POST_VARS["loginUsername"]) && empty($HTTP_POST_VARS["loginPassword"])))to
if ((empty($loginUsername) && !empty($loginPassword)) || (!empty($loginUsername) && empty($loginPassword)))and
$message = "Both a username and password must be supplied.";to
$_SESSION["message"] = "Both a username and password must be supplied.";
else { // Register a message to show the user session_register("message"); $message = "Error: you are not logged in!"; } // Redirect the browser back to the calling page if (session_is_registered("referer")) { // Delete the redirection session variable session_unregister("referer"); // Then, use it to redirect to the calling page header("Location: $referer"); exit;to
else { // Register a message to show the user session_register("message"); $_SESSION["message"] = "Error: you are not logged in!"; } // Redirect the browser back to the calling page if (session_is_registered("referer")) { // Then, use it to redirect to the calling page header("Location: {$_SESSION["referer"]}"); // Delete the redirection session variable session_unregister("referer"); exit;
global $order_no;Added:
$order_no = $_SESSION["order_no"];Removed:
global $message;Changed:
echo "<h3><font color=\"red\">$message</font></h3>";to
echo "<h3><font color=\"red\">{$_SESSION["message"]}</font></h3>";Removed:
$message = "";and
global $loginUsername;Changed:
echo "<p align=\"right\">You are currently logged in as <b>$loginUsername</b></p>\n";to
echo "<p align=\"right\">You are currently logged in as <b>{$_SESSION["loginUsername"]}</b></p>\n";
error_log($errorString, 1, "hugh");to
error_log($errorString, 1, "youremail@youraddress.com");
global $order_no;and changed:
"AND order_id = " . $order_no;to
"AND order_id = " . $_SESSION["order_no"];
if (empty($wineId) && empty($qty))to
if (empty($_GET["wineId"]) && empty($_GET["qty"]))and
$message = "Incorrect parameters to example.cart.3.php";to
$_SESSION["message"] = "Incorrect parameters to example.cart.3.php";and
header("Location: $HTTP_REFERER");to
header("Location: {$_SERVER["HTTP_REFERER"]}");and
$wineId = clean($wineId, 5); $qty = clean($qty, 3);to
$wineId = clean($_GET["wineId"], 5); $qty = clean($_GET["qty"], 3);and added:
$_SESSION["order_no"] = $order_no;and added:
// Initialise a local $order_no $order_no = $_SESSION["order_no"];and changed:
$message = "Sorry! We just sold out of this great wine!";to
$_SESSION["message"] = "Sorry! We just sold out of this great wine!";and changed:
header("Location: $HTTP_REFERER");to
header("Location: {$_SERVER["HTTP_REFERER"]}");
// Initialise a local $order_no $order_no = $_SESSION["order_no"];and updated:
$message = "There is nothing in your cart.";to
$_SESSION["message"] = "There is nothing in your cart.";and
header("Location: $referer");to
header("Location: {$_SESSION["referer"]}");and
header("Location: $HTTP_REFERER");to
header("Location: {$_SERVER["HTTP_REFERER"]}");
foreach($HTTP_GET_VARS as $varname => $value) $parameters[$varname] = clean($value, 4);to
foreach($_GET as $varname => $value) $parameters[$varname] = clean($value, 4);Added:
// Register a local $order_no $order_no = $_SESSION["order_no"];Changed:
$message = "Incorrect parameters to example.cart.6.php";to
$_SESSION["message"] = "Incorrect parameters to example.cart.6.php";and
header("Location: $HTTP_REFERER");to
header("Location: {$_SERVER["HTTP_REFERER"]}");and
$message = "There was an error updating your quantities. Try again.";to
$_SESSION["message"] = "There was an error updating your quantities. Try again.";and
$message = "There was an error updating quantities. Try again.";to
$_SESSION["message"] = "There was an error updating quantities. Try again.";
foreach($HTTP_GET_VARS as $varname => $value)to
foreach($_GET as $varname => $value)and
header("Location: example.cart.6.php?$QUERY_STRING");to
header("Location: example.cart.6.php?{$_SERVER["QUERY_STRING"]}");and
header("Location: example.search.1.php?$QUERY_STRING");to
header("Location: example.search.1.php?{$_SERVER["QUERY_STRING"]}");and
$referer = $HTTP_REFERER;to
$_SESSION["referer"] = $_SERVER["HTTP_REFERER"];and
header("Location: example.order.1.php?$QUERY_STRING");to
header("Location: example.order.1.php?{$_SERVER["QUERY_STRING"]}");and
$referer = $HTTP_REFERER;to
$_SESSION["referer"] = $_SERVER["HTTP_REFERER"];and
header("Location: example.order.2.php?$QUERY_STRING");to
header("Location: example.order.2.php?{$_SERVER["QUERY_STRING"]}");and
header("Location: example.order.3.php?$QUERY_STRING");to
header("Location: example.order.3.php?{$_SERVER["QUERY_STRING"]}");
$message = "There are no items in your shopping cart!";to
$_SESSION["message"] = "There are no items in your shopping cart!";and
header("Location: $HTTP_REFERER");to
header("Location: {$_SERVER["HTTP_REFERER"]}");and added:
// Initialise a local $order_no $order_no = $_SESSION["order_no"];and changed:
$message = "You must login to finalise your purchase.";to
$_SESSION["message"] = "You must login to finalise your purchase.";and
header("Location: $HTTP_REFERER");to
header("Location: {$_SERVER["HTTP_REFERER"]}");and
$custID = getCustomerID($loginUsername, NULL);to
$custID = getCustomerID($_SESSION["loginUsername"], NULL);Deleted:
if (!session_is_registered("message")) session_register("message");and added:
{ if (!session_is_registered("message")) session_register("message"); $_SESSION["message"] = $message; }
$message = "You must login to finalise your purchase.";to
$_SESSION["message"] = "You must login to finalise your purchase.";and
$message = "Incorrect parameters to example.shipping.1.php";to
$_SESSION["message"] = "Incorrect parameters to example.shipping.1.php";and added:
// Clean the GET variables $custID = clean($_GET["custID"], 5); $orderID = clean($_GET["orderID"], 5);and changed:
if ($custID != getCustomerID($loginUsername, NULL))to
if ($custID != getCustomerID($_SESSION["loginUsername"], NULL))and
$message = "You can only view your own receipts!";to
$_SESSION["message"] = "You can only view your own receipts!";
$message = "You must login to view your receipt.";to
$_SESSION["message"] = "You must login to view your receipt.";and
$message = "Incorrect parameters to example.shipping.2.php";to
$_SESSION["message"] = "Incorrect parameters to example.shipping.2.php";and added:
// Clean the GET variables $custID = clean($_GET["custID"], 5); $orderID = clean($_GET["orderID"], 5);and changed:
if ($custID != getCustomerID($loginUsername, NULL))to
if ($custID != getCustomerID($_SESSION["loginUsername"], NULL))and
$message = "You can only view your own receipts!";to
$_SESSION["message"] = "You can only view your own receipts!";
$message = "You must login to view your receipt.";to
$_SESSION["message"] = "You must login to view your receipt.";and added:
// Clean the GET variables $custID = clean($_GET["custID"], 5); $orderID = clean($_GET["orderID"], 5);and changed:
$message = "Incorrect parameters to example.shipping.3.php";to
$_SESSION["message"] = "Incorrect parameters to example.shipping.3.php";and
header("Location: $HTTP_REFERER");to
header("Location: {$_SERVER["HTTP_REFERER"]}");and
$message = "You can only view your own receipts!";to
$_SESSION["message"] = "You can only view your own receipts!";
if (!empty($HTTP_GET_VARS["regionName"])) $regionName = clean($regionName, 30);to
if (!empty($_GET["regionName"])) $regionName = clean($_GET["regionName"], 30);and
$regionName = $sessionRegionName;to
$regionName = $_SESSION["sessionRegionName"];and
if (!empty($HTTP_GET_VARS["wineType"])) $wineType = clean($wineType, 20);to
if (!empty($_GET["wineType"])) $wineType = clean($_GET["wineType"], 20);and
$wineType = $sessionWineType;to
$wineType = $_SESSION["sessionWineType"];and
if (!empty($HTTP_GET_VARS["offset"])) $offset = clean($offset, 5);to
if (!empty($_GET["offset"])) $offset = clean($_GET["offset"], 5);and
// Save the search criteria $sessionRegionName = $regionName; $sessionWineType = $wineType;to
// Save the search criteria $_SESSION["sessionRegionName"] = $regionName; $_SESSION["sessionWineType"] = $wineType;
$count = 0; $start = time();to
$_SESSION["count"] = 0; $_SESSION["start"] = time();and
$count++;to
$_SESSION["count"]++;and
(<?=$sessionId ?>) <br>count = <?=$count ?>. <br>start = <?=$start ?>.to
(<?php echo $sessionId; ?>) <br>count = <?php echo $_SESSION["count"]; ?>. <br>start = <?php echo $_SESSION["start"]; ?>.and
$duration = time() - $start;to
$duration = time() - $_SESSION["start"];
$count = 0; $start = time();to
$_SESSION["count"] = 0; $_SESSION["start"] = time();and
$count++;to
$_SESSION["count"]++;and
(<?=$sessionId ?>) <br>count = <?=$count ?>. <br>start = <?=$start ?>.to
(<?php echo $sessionId; ?>) <br>count = <?php echo $_SESSION["count"]; ?>. <br>start = <?php echo $_SESSION["start"]; ?>.and
$duration = time() - $start;to
$duration = time() - $_SESSION["start"];