This is a model answer to the question on the CS 4320 Test for 30 November 2004. Note that, because the question did not ask for any style handling, none is provided in this answer. However, a realistic program would have to include style handling.
Also note that, since the question did not specify a URL for the main site, you could have chosen any URL for it; this answer uses "main.php" but you could have used something else. Finally, note that this is not a realistic login program -- it actually provides no access control to "main.php"; for that, something more (such as a cookie or some other method) would have to be used.
Of the marks for this answer, 50% are for the PHP and 50% for the HTML.
<!-- This program is called login.php. -->
<html>
<?php
if (($loginName) && ($password))
{//The user has returned a completed login form
$db = mysql_connect("localhost","sarsfield","ballyneety");
mysql_select_db("clubDatabase",$db);
$list = mysql_query("select name from members where
loginName='$loginName' and
password='$password'",$db);
if ($row=mysql_fetch_array($list))
{//The user supplied a correct password and login name
?>
<head><title>Welcome</title></head>
<body>
<h1>Welcome <?php echo $name; ?></h1>
<p><a href="main.php">Click here to enter main site</a>.</p>
<?php
}
else
{//The user supplied incorrect data
?>
<head><title>Invalid login</title></head>
<body>
<h1>Invalid login</h1>
<p>Either your login name or password was incorrect.
<a href="login.php">Click here to try again</a>.</p>
<?php
}
}
else
{// Deliver a blank login form to the user's browser
?>
<head><title>Club Login</title></head>
<body>
<form method="post" action="login.php">
<h1>Club Login</h1>
<fieldset>
<legend>Your login name</legend>
<input type="text" name="loginName">
</fieldset>
<fieldset>
<legend>Your Password</legend>
<input type="password" name="password">
</fieldset>
<button type="submit">Login</button>
</form>
<?php
}
?>
</body>
</html>