The code for the form to be utilized for this assignment is shown
below. This needs to be saved as a PHP file (e.g.,
lab16_form.php).
<!--
Mark Motl
CS 4312
Lab 16
-->
<?php
/* Check the protocol for this request. Redirect an HTTP request to
an HTTPS request.
Ref.: https://stackoverflow.com/questions/5106313/redirecting-from-http-to-https-with-php
*/
if (empty($_SERVER['HTTPS']) || $_SERVER['HTTPS'] === "off")
{
$location = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
header('HTTP/1.1 301 Moved Permanently');
header('Location: ' . $location);
exit;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Lab 16 Day Number</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
</head>
<body>
<h1>Lab 16 Day Number</h1>
<p>
This form allows a user to enter a month, day, and year. The
associated PHP application determines the day number of the given
date.
</p>
<form action="lab16.php" method="post">
<p>
Select the month
</p>
<table>
<?php
$monthNames = array("January", "February", "March", "April", "May",
"June", "July", "August", "September",
"October", "November", "December");
foreach ($monthNames as $i => $month)
{
if ($i % 4 == 0)
echo "<tr>\n";
echo "<td><input type=\"radio\" name=\"month\" value=\"", $i + 1, "\"";
if ($i == 0)
echo " checked=\"checked\"";
echo " />$month</td>\n";
if (($i + 1) % 4 == 0)
echo"</tr>\n";
}
?>
</table>
<hr />
<p>
Select the day of the month
<select size="1" name="day">
<?php
for ($i = 1; $i<=31; ++$i)
{
echo "<option";
if ($i == 1)
echo " selected=\"selected\"";
echo ">$i</option>\n";
}
?>
</select>
</p>
<hr />
<p>
Enter the year
<input type="text" name="year" size="4" maxlength="4" /><br /><br />
<input type="reset" value="Clear This Form" />
<input type="submit" name="Submit" value="Submit This Form" />
</p>
</form>
<p>
<?php
$location = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$location = urlencode($location);
echo '<a href="https://validator.w3.org/nu/?doc=' . $location . '">';
?>
<img src="https://www.w3.org/QA/Tools/I_heart_validator"
alt="I heart Validator logo" height="31" width="80" />
</a>
</p>
</body>
</html>
I would suggest using the following leap year form and its
associated PHP application (both shown below) when completing
this assignment.
The code for the HTML form is shown below:
<!--
Mark Motl
CS 4312
Leap Year Example
-->
<?php
/* Check the protocol for this request. Redirect an HTTP request to
an HTTPS request.
Ref.: https://stackoverflow.com/questions/5106313/redirecting-from-http-to-https-with-php
*/
if (empty($_SERVER['HTTPS']) || $_SERVER['HTTPS'] === "off")
{
$location = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
header('HTTP/1.1 301 Moved Permanently');
header('Location: ' . $location);
exit;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Leap Year</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
</head>
<body>
<h1>Leap Year</h1>
<p>
This is a sample form that allows a user to enter a text value that
represents a year. The associated PHP application determines if the
input year was/is/will be a leap year.
</p>
<p>
A leap year is defined as a year in the Gregorian calendar
containing 366 days with February 29<sup>th</sup> as the extra
day. Leap years are those years divisible by 4, except those years
that complete centuries (that is, divisible by 100) which must be
divisible by 400. Thus, 1998 was not a leap year, 1996 was, 2000
was, 1900 was not, 1600 was, 2100 will not be, 2028 will be, etc.
</p>
<form action="leapyear.php" method="post">
<p>
Enter the year
<input type="text" name="year" size="4" maxlength="4" /><br /></p>
<p><input type="reset" value="Clear This Form" />
<input type="submit" name="Submit" value="Submit This Form" />
</p>
</form>
<p>
<?php
$location = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$location = urlencode($location);
echo '<a href="https://validator.w3.org/nu/?doc=' . $location . '">';
?>
<img src="https://www.w3.org/QA/Tools/I_heart_validator"
alt="I heart Validator logo" height="31" width="80" />
</a>
</p>
</body>
</html>
The code for the PHP application program is shown below:
<!--
Mark Motl
CS 4312
Leap Year Example
-->
<?php
/* Check the protocol for this request. Redirect an HTTP request to
an HTTPS request.
Ref.: https://stackoverflow.com/questions/5106313/redirecting-from-http-to-https-with-php
*/
if (empty($_SERVER['HTTPS']) || $_SERVER['HTTPS'] === "off")
{
$location = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
header('HTTP/1.1 301 Moved Permanently');
header('Location: ' . $location);
exit;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Leap Year</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
</head>
<body>
<?php
/* ------------------- Function: ParseFormElements ----- */
function parseFormElements($name)
{
if (strcasecmp($_SERVER['REQUEST_METHOD'], "get") == 0)
{
if (strstr($_SERVER['QUERY_STRING'], $name) != NULL)
if ($_GET[$name] == NULL)
errorMsg("variable " . $name . " is empty");
else
return htmlspecialchars($_GET[$name]);
else
errorMsg("variable " . $name . " is missing");
}
elseif (strcasecmp($_SERVER['REQUEST_METHOD'], "post") == 0)
{
if ($_POST[$name] == NULL)
errorMsg("variable " . $name . " is empty");
else
return htmlspecialchars($_POST[$name]);
}
else
errorMsg("unknown REQUEST_METHOD");
}
/* ------------------- Function: errorMsg -------------- */
function errorMsg($msg)
{
echo "<p>Your form results could not be processed because "
. $msg . ".</p>\n";
writeClosing();
exit(1);
}
echo "<p>Mark Motl - CS 4312 - Leap Year Example<br /></p>\n";
$year = parseFormElements("year");
if (!preg_match("/^[0-9]{1,4}$/", $year))
errorMsg("the value entered for the year, namely, $year is invalid");
// Ensure the year is in the Gregorian calendar
if ($year <= 1582)
errorMsg("the value entered for the year, namely, $year is not in the "
. "Gregorian calendar. Use a year later than 1582.");
echo "<p>$year ";
date_default_timezone_set('America/Chicago');
$myTimeArray = getdate();
if ($year % 400 == 0 || ($year % 4 == 0 && $year % 100 != 0))
{
if ($year < $myTimeArray['year'])
echo "was ";
else if ($year == $myTimeArray['year'])
echo "is ";
else
echo "will be ";
}
else
{
if ($year < $myTimeArray['year'])
echo "was not ";
else if ($year == $myTimeArray['year'])
echo "is not ";
else
echo "will not be ";
}
echo "a leap year.<br /></p>\n";
writeClosing();
?>
<?php
/* ------------------- Function: writeClosing ---------- */
function writeClosing()
{
?>
<p>
<?php
$location = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$location = urlencode($location);
echo '<a href="https://validator.w3.org/nu/?doc=' . $location . '">';
?>
<img src="https://www.w3.org/QA/Tools/I_heart_validator"
alt="I heart Validator logo" height="31" width="80" />
</a>
</p>
</body>
</html>
<?php
}
?>