C.7 Chapter 8
C.7.1 Exercise 1:
<?php
$page_count = $_COOKIE['page_count'] + 1;
setcookie('page_count',$page_count);
print "Number of views: $page_count";
?>
C.7.2 Exercise 2:
<?php
$page_count = $_COOKIE['page_count'] + 1;
if ($page_count = = 20) {
// an empty value deletes the cookie
setcookie('page_count','');
print "Time to start over.";
} else {
setcookie('page_count', $page_count);
print "Number of views: $page_count";
if ($page_count = = 5) {
print "<br/> This is your fifth visit.";
} elseif ($page_count = = 10) {
print "<br/> This is your tenth visit. Aren't you sick of this page yet?";
} elseif ($page_count = = 15) {
print "<br/> This is your fifteenth visit. Don't you have anything better to
do?";
}
}
?>
C.7.3 Exercise 3:
Here is the color selection form page:
<?php
require 'formhelpers.php';
session_start( );
$colors = array('#ff0000' => 'red',
'#ff6600' => 'orange',
'#ffff00' => 'yellow',
'#0000ff' => 'green',
'#00ff00' => 'blue',
'#ff00ff' => 'purple');
if ($_POST['_submit_check']) {
if ($form_errors = validate_form( )) {
show_form($form_errors);
} else {
process_form( );
}
} else {
show_form( );
}
function show_form($errors = '') {
print '<form method="POST" action="'.$_SERVER['PHP_SELF'].'">';
if ($errors) {
print '<ul><li>';
print implode('</li><li>',$errors);
print '</li></ul>';
}
// Since we're not supplying any defaults of our own, it's OK
// to pass $_POST as the defaults array to input_select and
// input_text so that any user-entered values are preserved
print 'Color: ';
input_select('color', $_POST, $GLOBALS['colors']);
print '<br/>';
input_submit('submit','Select Color');
print '<input type="hidden" name="_submit_check" value="1"/>';
print '</form>';
}
function validate_form( ) {
$errors = array( );
// The dish selected in the menu must be valid
if (! array_key_exists($_POST['color'], $GLOBALS['colors'])) {
$errors[ ] = 'Please select a valid color.';
}
return $errors;
}
function process_form( ) {
$_SESSION['color'] = $_POST['color'];
print "Your favorite color is: " . $GLOBALS['colors'][ $_SESSION['color'] ];
}
?>
And here is the background-color-changing page:
<?php
session_start( );
print <<<_HTML_
<html>
<body bgcolor="$_SESSION[color]">
This page has your personalized background color.
</body>
</html>
_HTML_;
?>
C.7.4 Exercise 4:
Here's the order form page:
<?php
session_start( );
require 'formhelpers.php';
$products = array('cuke' => 'Braised Sea Cucumber',
'stomach' => "Sauteed Pig's Stomach",
'tripe' => 'Sauteed Tripe with Wine Sauce',
'taro' => 'Stewed Pork with Taro',
'giblets' => 'Baked Giblets with Salt',
'abalone' => 'Abalone with Marrow and Duck Feet');
if ($_POST['_submit_check']) {
if ($form_errors = validate_form( )) {
show_form($form_errors);
} else {
process_form( );
}
} else {
show_form( );
}
function show_form($errors = '') {
global $products;
print '<form method="POST" action="'.$_SERVER['PHP_SELF'].'">';
if ($errors) {
print '<ul><li>';
print implode('</li><li>',$errors);
print '</li></ul>';
}
// Build up an array of defaults if there is an order saved
// in the session
if ($_SESSION['saved_order']) {
$defaults = array( );
foreach ($products as $product => $description) {
$defaults["dish_$product"] = $_SESSION["dish_$product"];
}
} else {
$defaults = $_POST;
}
foreach ($products as $product => $description) {
input_text("dish_$product", $defaults);
print " $description<br/>";
}
input_submit('submit','Order');
print '<input type="hidden" name="_submit_check" value="1"/>';
print '</form>';
}
function validate_form( ) {
global $products;
$errors = array( );
foreach ($products as $product => $description) {
// If something was entered in the text box
if (strlen($_POST["dish_$product"]) &&
// And it's not a valid integer
(($_POST["dish_$product"] != strval(intval($_POST["dish_$product"]))) ||
// Or it's less than zero
intval($_POST["dish_$product"]) < 0)) {
// Then it's an error
$errors[ ] = "Please enter a valid quantity for $description.";
}
}
return $errors;
}
function process_form( ) {
global $products;
$_SESSION['saved_order'] = 1;
foreach ($products as $product => $description) {
if (strlen($_POST["dish_$product"])) {
$_SESSION["dish_$product"] = $_POST["dish_$product"];
}
}
print 'Thank you for your order.';
}
?>
Here's the check-out page:
<?php
session_start( );
require 'formhelpers.php';
$products = array('cuke' => 'Braised Sea Cucumber',
'stomach' => "Sauteed Pig's Stomach",
'tripe' => 'Sauteed Tripe with Wine Sauce',
'taro' => 'Stewed Pork with Taro',
'giblets' => 'Baked Giblets with Salt',
'abalone' => 'Abalone with Marrow and Duck Feet');
// Since the form just consists of one button, there's no need
// to validate the submitted form data
if ($_POST['_submit_check']) {
process_form( );
} else {
show_form( );
}
function show_form($errors = '') {
global $products;
if ($_SESSION['saved_order']) {
print 'Your order: <ul>';
foreach ($products as $product => $description) {
if (array_key_exists("dish_$product", $_SESSION)) {
print '<li> '.$_SESSION["dish_$product"]." $description </li>";
}
}
print '</ul>';
} else {
print 'There is no saved order.';
}
print '<br/>';
// This assumes that the order form page is saved as "orderform.php"
print '<a href="orderform.php">Return to Order Page</a>';
print '<br/>';
print '<form method="POST" action="'.$_SERVER['PHP_SELF'].'">';
input_submit('submit','Check Out');
print '<input type="hidden" name="_submit_check" value="1"/>';
print '</form>';
}
function process_form( ) {
global $products;
unset($_SESSION['saved_order']);
foreach ($products as $product => $description) {
unset($_SESSION["dish_$product"]);
}
print 'Your order has been cleared.';
}
?>
|