Register.php

<?php
   
require ('SSLCheck.php');
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
   <title>User Registration</title>
   <link rel='stylesheet' type='text/css' href='/Styles.css' media='all'>
   <style type='text/css'>
      <!--
      body
         {
         background-color:#ffffff;
         color:#000000;
         }
      table.normal
         {
         margin-left:auto; width:490px; margin-right:auto;
         }
      table.normal td
         {
         border-style:none;
         }
      table.normal td.col1
         {
         width:145px;
         text-align:right;
         vertical-align:top;
         }
      table.normal td.col2
         {
         width:345px;
         text-align:left;
         }
      table.normal td.foot
         {
         text-align:center;
         }
      input
         {
         width:330px;
         }
      div.prompt
         {
         font-size:.65em;
         width:330px;
         }
      -->
   </style>
</head>
<body>
   <?php
      $showForm 
true;
      
$emailBackground '#ffffff';
      
$usernameBackground '#ffffff';
      
$passwordBackground '#ffffff';
      if (
count ($_POST) > 0)
      
/* form data has been submitted */
         
{
         
/* trim field data: */
         
$emailField trim ($_POST['emailField']);
         
$usernameField trim ($_POST['usernameField']);
         
$passwordField trim ($_POST['passwordField']);
         
$passwordField2 trim ($_POST['passwordField2']);

         
/* check that form data is valid: */
         
$validData true;
         if (
$emailField == "")
            {
            echo 
"<h3 style='color:red'>Error: Please enter your Email Address.</h3>";
            
$emailBackground '#ffff00';
            
$validData false;
            }
         if (
$usernameField == "")
            {
            echo 
"<h3 style='color:red'>Error: Please enter a Username.</h3>";
            
$usernameBackground '#ffff00';
            
$validData false;
            }

         if (
$passwordField == "")
            {
            echo 
"<h3 style='color:red'>Error: Please enter a Password.</h3>";
            
$passwordBackground '#ffff00';
            
$validData false;
            }
         else if (
strlen ($passwordField) < 6)
            {
            echo 
"<h3 style='color:red'>Error: Your Password must be at least 6 characters long. Please reenter.</h3>";
            
$passwordBackground '#ffff00';
            
$validData false;
            }
         else if (
$passwordField != $passwordField2)
            {
            echo 
"<h3 style='color:red'>Error: The passwords you entered do not match. Please reenter.</h3>";
            
$passwordBackground '#ffff00';
            
$validData false;
            }
         
/* attempt to write field data to database -- if username already exists, show error message -- if data
         successfully written show confirmation message and a link to user's original page, and then set $showForm to
         false: */
         
if ($validData)
            {
            @ 
$db = new mysqli ('localhost''username''password''database');
            if (
mysqli_connect_errno ())
               {
               echo
                  
"<h3 style='color:red; text-align:center;'>
                     Database connection error: "
.mysqli_connect_error ()."<br>Please try again later.
                  </h3></body></html>"
;
               exit;
               }
            
$query "insert into passwords values
                     ('$emailField', '$usernameField', SHA1('$passwordField'))"
;
            
$result $db->query($query);
            if (!
$result)
               {
               if (
$db->errno == 1062)
                  {
                  echo 
"<h3 style='color:red'>Error: The Username you chose is already in use. Please enter another
                  Username.</h3>"
;
                  
$usernameBackground '#ffff00';
                  }
               else
                  {
                  echo
                     
"<h3 style='color:red; text-align:center;'>
                        Error saving registration information in database:<br>
                        "
.$db->error."
                     </h3></body></html>"
;
                  
$db->close();
                  exit;
                  }
               }
            else
               {
               echo
                  
"<div style='text-align:center'>
                      <h3>Your registration information was saved. Thank you!</h3>"
;
               if (isset (
$_GET["url"]))
                  {
                  
$URL $_GET["url"];
                  echo 
"<a href='".$URL."'>Return to private page you were accessing.</a>";
                  }
               echo 
"</div>";
               
$showForm false;
               }
            
$db->close();
            }
         }
      if (
$showForm)
         {
         if (isset (
$_GET["url"]))
            
$queryString "?url=".$_GET["url"];
         echo
            
"<form name='messageForm' action='Register.php".$queryString."' method='post'
            enctype='application/x-www-form-urlencoded'>
               <h3 class='title'>Registration Form</h3>
               <table class='normal'>
                  <tr>
                     <td class='col1' style='padding-top:15px;'>Email Address:</td>
                     <td class='col2' style='padding-top:15px;'>
                        <input style='background-color:$emailBackground;' type='text' name='emailField'
                        value='$emailField' maxlength='80'>
                        <div class='prompt'>
                           80 characters max
                        </div>
                     </td>
                  </tr>
                  <tr>
                     <td class='col1'>Username:</td>
                     <td class='col2'>
                        <input style='background-color:$usernameBackground;' type='text' name='usernameField'
                        value='$usernameField' maxlength='16'>
                        <div class='prompt'>
                           ID to be entered into 'User name' field when prompted by your browser -- 16 characters max
                        </div>
                     </td>
                  </tr>
                  <tr>
                     <td class='col1'>Password:</td>
                     <td class='col2'>
                        <input style='background-color:$passwordBackground;' type='password' name='passwordField'
                        value='$passwordField' maxlength='16'>
                        <div class='prompt'>
                           Password to be entered into 'Password' field when prompted by your browser -- between 6
                           and 16 characters
                        </div>
                     </td>
                  </tr>
                  <tr>
                     <td class='col1'>Reenter Password:</td>
                     <td class='col2'>
                        <input style='background-color:$passwordBackground;' type='password' name='passwordField2'
                        value='$passwordField2' maxlength='16'>
                     </td>
                  </tr>
                  <tr>
                     <td colspan='2' class='foot'>
                        <button type='submit'>Submit Registration</button>
                        <button type='reset' onclick='return confirm(\"Reverse all edits you have made?\");'>Reset
                        Form</button>
                     </td>
                  </tr>
               </table>
            </form>\n"
;
         }
   
?>
</body>
</html>