<?php
session_start();
?>
<!DOCTYPE html>
<html>
<!-- try-trio.php
playing with session variables to control a multi-page application -
includes a common complaint-page function
by: Sharon Tuttle
last modified: 4-25-13
-->
<head>
<title> try-trio.php </title>
<meta charset="utf-8" />
</head>
<body>
<?php
// in a multi-page PHP document, it is BETTER STYLE to
// handle each page/case in a separate function --
// see how much easier, here, it is to get an idea
// of the "overall" application flow?
if (!isset($_SESSION['next_page']))
{
request_name();
}
elseif ($_SESSION['next_page'] == "quest")
{
request_quest();
}
elseif ($_SESSION['next_page'] == "color")
{
request_color();
}
elseif ($_SESSION['next_page'] == "farewell")
{
show_farewell();
}
else // was it anything else?! request name...
{
request_name();
}
//-----
// function: request_name
// purpose: expects nothing, and makes a form to ask for user's name
//-----
function request_name()
{
print <<<PAGE_NAME
<h1> WHAT is your NAME? </h1>
<form method="post" action="$_SERVER[PHP_SELF]">
<p>
<input type="text" name="name" />
<input type="submit" value="Submit it" />
</p>
</form>
PAGE_NAME;
$_SESSION['next_page'] = "quest";
}
//-----
// function: make_complaint
// purpose: expects a string saying what should have been
// entered that wasn't, and prints a complaint screen
// complaining about this, destroying the current
// session so that the submit button will lead
// again to the opening screen
//-----
function make_complaint($missing_info_type)
{
print <<<RUN_AWAY
<h1> Need to enter a $missing_info_type...! </h1>
<form method="post" action="$_SERVER[PHP_SELF]">
<p>
<input type="submit" value="Try again!" />
</p>
</form>
RUN_AWAY;
session_destroy();
}
//-----
// function: request_quest
// purpose: expects nothing, read user's name from form requesting
// this action, and use that name in creating a form asking for
// his/her quest
//-----
function request_quest()
{
// if get here -- $_POST['name'] should be set, right?
if (!isset($_POST['name']) || ($_POST['name'] == ""))
{
make_complaint("name");
}
// otherwise, it should be safe to proceed...?
else
{
$name = htmlspecialchars($_POST['name']);
$_SESSION['name'] = $name;
print <<<PAGE_QUEST
<h1> Well, $name, WHAT is your quest? </h1>
<form method="post" action="$_SERVER[PHP_SELF]">
<p>
<input type="text" name="quest" />
<input type="submit" value="Submit it" />
</p>
</form>
PAGE_QUEST;
$_SESSION['next_page'] = "color";
}
}
//-----
// function: request_color
// purpose: expects nothing, read user's quest from form requesting
// this action, and uses name from earlier in the session and
// this new quest in creating a form asking for his/her
// favorite color
//-----
function request_color()
{
// if get here -- $_POST['quest'] should be set, right?
if (!isset($_POST['quest']) || ($_POST['quest'] == ""))
{
make_complaint("quest! Run Away");
}
// otherwise, it should be safe to proceed...?
else
{
$name = $_SESSION['name'];
$quest = htmlspecialchars($_POST['quest']);
$_SESSION['quest'] = $quest;
print <<<PAGE_COLOR
<h1> AHA - $name, <br />
your quest: $quest <br />
is NOBLE indeed! <br /><br />
BUT - WHAT is your FAVORITE COLOR? </h1>
<form method="post" action="$_SERVER[PHP_SELF]">
<p>
<input type="text" name="color" />
<input type="submit" value="Submit it" />
</p>
</form>
PAGE_COLOR;
$_SESSION['next_page'] = "farewell";
}
}
//-----
// function: show_farewell
// purpose: expects nothing, read user's color from form requesting
// this action, and use name and quest from earlier in the session
// and this new color in creating a form bidding this user farewell.
//-----
function show_farewell()
{
// if get here -- $_POST['color'] should be set, right?
if (!isset($_POST['color']) || ($_POST['color'] == ""))
{
make_complaint("color! Run Away");
}
// otherwise, it should be safe to proceed...?
else
{
$name = $_SESSION['name'];
$quest = $_SESSION['quest'];
$color = strip_tags($_POST['color']);
print <<<PAGE_FAREWELL
<h1> AHA - $name, <br />
lover of the gentle hue of $color, <br />
I let you pass, and wish you WELL on your quest: <br />
$quest
</h1>
<form method="post" action="$_SERVER[PHP_SELF]">
<p>
<input type="submit" value="Return to Beginning" />
</p>
</form>
PAGE_FAREWELL;
session_destroy();
}
}
require("my-std-footer.html");
?>