<html>
<! BasicFileLogin.php                                         >
<! Chapter 11, Oracle Database XE PHP Web Programming         >
<! by Michael McLaughlin                                      >
<!                                                            >
<! This demonstrates a basic HTTP authentication against a    >
<! password file.                                             >
<head>
<title>
  Chapter 11 : BasicFileLogin.php
</title>
<style type="text/css">
  div#centered {border:0;height:50%;top:25%;left:25%;width:50%;
                position:absolute;}
</style>
</head>
<body>
<?php
  // Set initial state for invalid credentials.
  $valid_user = false;

  // Check for HTTP basic authentication variables.
  if ((isset($_SERVER['PHP_AUTH_USER'])) &&
      (isset($_SERVER['PHP_AUTH_PW'])))
  {
    // Read the file into an array of strings.
    $passwords = file("simplepassword.txt");

    // Read through the value list.
    foreach ($passwords as $credentials)
    {
	  // Break the delimited lines into scalar variables.
      list($userid,$password) = explode(":",$credentials);

	  // Find a match and exit.
      if (($_SERVER['PHP_AUTH_USER'] == $userid) &&
          (md5($_SERVER['PHP_AUTH_PW']) == trim($password)))
      {
	    // Reset invalid credential state variable.
        $valid_user = true;
		break;
      }
    }
  }

  // When credentials are invalid prompt for them.
  if (!$valid_user)
  {
    // Send headers to force basic HTTP authentication.
    header('WWW-Authenticate: Basic Realm="www.techtinker.com"');
	header('HTTP/1.0 401 Unauthorized');

	// Print failed validation message.
    print "<html><body><font size=+2>";
	print "The site has logged your [".$_SERVER['REMOTE_ADDR']."] IP ";
    print "address, please enter a valid user and password.";
	print "</font></body></html>";
    exit;
  }
  else
    // Print successful validation message.
    enter_credentials("BasicFileWrite.php");

  // Return a dynamic entry form.
  function enter_credentials($form_target)
  {
    $out  = '<div id=centered>';
    $out .= '<form method="post" action="'.$form_target.'">';
    $out .= '<table border="4" bordercolor="silver" cellspacing="0">';
    $out .= '<tr><td align="center" width="400">';
    $out .= '<font size=+3>New User</font>';
    $out .= '</td></tr>';
    $out .= '<tr><td>';
    $out .= '<table border="0" cellpadding="5" cellspacing="0">';
    $out .= '<tr>';
    $out .= '<td align="right" width="200">User ID:</td>';
    $out .= '<td width="200"><input name="userid" type="text"></td>';
    $out .= '</tr>';
    $out .= '<tr>';
    $out .= '<td align="right" width="200">User Password:</td>';
    $out .= '<td width="200"><input name="passwd" type="password"></td>';
    $out .= '</tr>';
    $out .= '</table>';
    $out .= '</td></tr>';
    $out .= '<tr><td align="center" colspan="2">';
    $out .= '<input name="submit" type="submit" value="Add User">';
    $out .= '</td></tr>';
    $out .= '</table>';
    $out .= '</form>';
    $out .= '</div>';

    // Render the form in a web page.
    print $out;
  }
?>
</body>
</html>
</body>
</html>