The following source program is an example PHP program to
determine if a year was/is/will be a leap year. The input year is
obtained from the end of the URL (everything after the ?).
<!--
Mark Motl
CS 4312
IsLeap 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>Is Leap</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
</head>
<body>
<p>
<?php
// Output an ID line
echo "Mark Motl - CS 4312 - IsLeap Example<br /><br />\n";
// Obtain the QUERY_STRING, if any, via which the page was accessed.
// It will be the information after the ? in the URL
$year = $_SERVER['QUERY_STRING'];
echo "$year " .
(is_numeric($year) // if $year is numeric
? ($year % 400 == 0 // then if $year is divisible by 400
? "was/is/will be a leap year" // then leap is true
: ($year % 4 == 0 // else if $year is divisible by 4
? ($year % 100 != 0 // then if $year is not divisible by 100
? "was/is/will be a leap year" // then leap is true
: "was not/is not/will not be a leap year") // else leap is false
: "was not/is not/will not be a leap year")) // else leap is false
: "is not numeric"); // else $year is not numeric
echo "<br />\n";
?>
</p>
<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>