<!DOCTYPE html>
<html> 
<head> 
  <title>User Output</title>
  <meta charset="utf-8" />
</head> 

<body>

<?php

// first define functions
  
function print_form() {

    echo <<<END

           <form action="
$_SERVER[PHP_SELF]" method="post">
           
           <h2 class="center">Exam 1 Part 5</h2> 
           <hr />
    Your user name:
    <input type="text" name="username" size="30" />
    Your Banner ID:
    <input type="text" name ="banner" size ="9" />
    Your password:
    <input type="password" name="pword" size="20" />

    <input type="hidden" name="stage" value="process" />

    <input type="submit" value="Submit" />
    <input type="reset" value="Clear" />
    
    </form>
    <hr />
END;

  }

function 
process_form() {

    
$username $_POST['username'];
    
$banner $_POST["banner"];
    
$pword $_POST["pword"];
    
    
# initialize error variable to null aka false
    
$error 0;

    
# if banner is something other than 9 digits or pword isn't the right
    # length, set error
    
if ((!preg_match('/^\d{9}$/'$banner) || !preg_match('/^.{8,14}$/'$pword))) {
        
$error 1;
    }

    
# if pword doesn't contain an alphabetic character and a digit or
    # it does contain a blank, set error
    
if (!preg_match('/[a-zA-Z].*[a-zA-Z]/'$pword) || !preg_match('/\d.*\d/'$pword)
        || 
preg_match('/ /'$pword)) {
        
$error 1;
    }

    
# if error is set, print an error message
    
if ($error) {
        print 
"<p>Your input is broken.  Please fix it.</p>";
    }
    
# otherwise print out the last 4 digits of banner in a header and
    # the other stuff in a paragraph
    
else {
        
$last4 substr($banner54);
    
# or could just do $banner[5] $banner[6] $banner[7] $banner[8]
        
print "<h1> $last4 </h1>";
        print 
"<p> username: $username</p>";
    }
}

# main program

if (isset($_POST['stage']) && ('process' == $_POST['stage'])) {
    
process_form();
} else {
    
print_form();
}

?>

</body>
</html>