Thanks to all that assisted. This works:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title></title>
<link rel="stylesheet" href="../docs/misc/styles.css" type="text/css">
</head>
<body>
<table style="height: 100%; text-align: center; width: 90%">
<tr>
<td style="vertical-align: middle">
<table>
<form method="post" action="<? echo $_SERVER['PHP_SELF'] ?>?action=login">
<tr>
<td><b>Login name:</b><br>
<input type="text" size="30" name="loginname"></td>
</tr>
<tr>
<td><b>Password:</b><br>
<input type="password" size="30" name="password"></td>
</tr>
<tr>
<td><p><? if (substr($_SERVER['PHP_SELF'],-9) == "login.php") { echo "<p>Never link directly to this file, always link to the protected file!</p>"; } else { echo "<input class=send type=submit value=\"Login!\">"; } ?></p></td>
</tr>
</form>
</table>
</td>
</tr>
</table>
</body>
</html>
protect.php
<?
$loginname = $_POST['loginname'];
$password = $_POST['password'];
$action = $_GET['action'];
$user_passwords = array (
"demo" => "demo"
);
$logout_page = "logout.php";
$login_page = "login.php";
$invalidlogin_page = "invalidlogin.php";
if ($action == "logout")
{
Setcookie("logincookie[pwd]","",time() -86400);
Setcookie("logincookie[user]","",time() - 86400);
include($logout_page);
exit;
}
else if ($action == "login")
{
if (($loginname == "") || ($password == ""))
{
include($invalidlogin_page);
exit;
}
else if (strcmp($user_passwords[$loginname],$password) == 0)
{
Setcookie("logincookie[pwd]",$password,time() + 86400);
Setcookie("logincookie[user]",$loginname,time() + 86400);
}
else
{
include($invalidlogin_page);
exit;
}
}
else
{
if (($_COOKIE['logincookie']['pwd'] == "") || ($_COOKIE['logincookie']['user'] == ""))
{
include($login_page);
exit;
}
else if ($user_passwords[$_COOKIE['logincookie']['user']] == $_COOKIE['logincookie']['pwd'])
{
Setcookie("logincookie[pwd]",$_COOKIE['logincookie']['pwd'],time() + 86400);
Setcookie("logincookie[user]",$_COOKIE['logincookie']['user'],time() + 86400);
}
else
{
include($login_page);
exit;
}
}
?>