Guest User

Untitled

a guest
Jan 16th, 2017
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.24 KB | None | 0 0
  1. <!DOCTYPE HTML>
  2. <html lang="en">
  3. <head><meta charset="UTF-8">
  4. <title>Registration</title>
  5. </head>
  6. <body>
  7.  
  8. <?php
  9.  
  10. $page_title = 'Register';
  11. include ('includes/header.html');
  12.  
  13. # conditional test to only execute contained statements if form has been submitted.
  14. if ($_SERVER['REQUEST_METHOD'] == 'POST')
  15. {
  16.  
  17. # statements to be inserted here.
  18.  
  19. # open database connection and initialise array for error messages.
  20. require ('../connect_db.php');
  21. $errors = array();
  22.  
  23. # stores error msg if firstname field remains empty, or store value in a variable.
  24. if (empty($_POST['firstname']))
  25. {$errors[] = 'Please enter your first name.';}
  26. else
  27. {$fn = mysqli_real_escape_string($dbc,
  28. trim($_POST['firstname']));}
  29.  
  30. # stores error msg if lastname field remains empty, or store value in a variable.
  31. if (empty($_POST['lastname']))
  32. {$errors[] = 'Please enter your last name.';}
  33. else
  34. {$ln = mysqli_real_escape_string($dbc,
  35. trim($_POST['lastname']));}
  36.  
  37. # stores error msg if address field remains empty, or store value in a variable.
  38. if (empty($_POST['address']))
  39. {$errors[] = 'Please enter your address.';}
  40. else
  41. {$a = mysqli_real_escape_string($dbc,
  42. trim($_POST['address']));}
  43.  
  44. # stores error msg if town field remains empty, or store value in a variable.
  45. if (empty($_POST['town']))
  46. {$errors[] = 'Please enter your town.';}
  47. else
  48. {$t = mysqli_real_escape_string($dbc,
  49. trim($_POST['town']));}
  50.  
  51. # stores error msg if postcode field remains empty, or store value in a variable.
  52. if (empty($_POST['postcode']))
  53. {$errors[] = 'Please enter your postcode.';}
  54. else
  55. {$pc = mysqli_real_escape_string($dbc,
  56. trim($_POST['postcode']));}
  57.  
  58. # stores error msg if email field remains empty, or store value in a variable.
  59. if (empty($_POST['email']))
  60. {$errors[] = 'Please enter your email.';}
  61. else
  62. {$e = mysqli_real_escape_string($dbc,
  63. trim($_POST['email']));}
  64.  
  65. # stores error msg if username field remains empty, or store value in a variable.
  66. if (empty($_POST['username']))
  67. {$errors[] = 'Please enter your username.';}
  68. else
  69. {$u = mysqli_real_escape_string($dbc,
  70. trim($_POST['username']));}
  71.  
  72. # stores password as a variable if both password fields match, or store an error msg if not matching or first field is empty.
  73. if (!empty($_POST['password1']))
  74. {
  75. if ($_POST['password1'] != $_POST['password2'])
  76. {$errors[] = 'Passwords do not match.';}
  77. else
  78. {$p = mysqli_real_escape_string($dbc,
  79. trim($_POST['password1']));}
  80. }
  81. else {$errors[] = 'Please enter your password.';}
  82.  
  83. # stores error msg if email already exists in database.
  84. if (empty($errors))
  85. {
  86. $q = "SELECT username FROM users WHERE email='$e'";
  87. $r = mysqli_query ($dbc,$q);
  88. if (mysqli_num_rows($r) != 0)
  89. {$errors[] = 'Email address already registered.
  90. <a href="login.php">Login</a>';}
  91. }
  92.  
  93. # stores user data in database and displays a confirmation message when registration is successful, closes the database connection and includes a page footer as well as exit the script.
  94. if (empty($errors))
  95. {
  96. $q = "INSERT INTO users
  97. (firstname, lastname, address, town, postcode, email, username, password)
  98. VALUES ('$fn', '$ln', '$a', '$t', '$pc', '$e', 'u', SHA1('$p'))";
  99. $r = mysqli_query ($dbc,$q);
  100.  
  101. if ($r)
  102. {
  103. echo '<h1>Registered!</h1>
  104. <p>You are now registered.</p>
  105. <p><a href="login.php">Login</a></p>';
  106. }
  107.  
  108. mysqli_close($dbc);
  109. include ('includes/footer.html');
  110. exit();
  111. }
  112.  
  113. # displays all stored error msg when registration fails and closes database connection.
  114. else
  115. {
  116. echo '<h1>Error!</h1>
  117. <p id="err_msg">The following error(s) occurred:<br>';
  118. foreach ($errors as $msg)
  119. {
  120. echo " -$msg<br>";
  121. }
  122. echo 'Please try again.</p>';
  123. mysqli_close($dbc);
  124. }
  125.  
  126. }
  127. ?>
  128.  
  129. <!--Sticky HTML form-->
  130. <h1>Register</h1>
  131. <form action="register.php" method="POST">
  132. <p>
  133. First Name: <input type="text" name="firstname"
  134. value="<?php if (isset($_POST['firstname']))
  135. echo $_POST['firstname'];?>">
  136. Last Name: <input type="text" name="lastname"
  137. value="<?php if (isset($_POST['lastname']))
  138. echo $_POST['lastname'];?>">
  139. </p><p>
  140. Address: <input type="text" name="address"
  141. value="<?php if (isset($_POST['address']))
  142. echo $_POST['address'];?>">
  143. Town: <input type="text" name="town"
  144. value="<?php if (isset($_POST['town']))
  145. echo $_POST['town'];?>">
  146. Postcode: <input type="text" name="postcode"
  147. value="<?php if (isset($_POST['postcode']))
  148. echo $_POST['postcode'];?>">
  149. </p><p>
  150. Email Address: <input type="text" name="email"
  151. value="<?php if (isset($_POST['email']))
  152. echo $_POST['email'];?>">
  153. </p><p>
  154. Username: <input type="text" name="username"
  155. value="<?php if (isset($_POST['username']))
  156. echo $_POST['username'];?>">
  157. </p><p>
  158. Password: <input type="password" name="password1"
  159. value="<?php if (isset($_POST['password1']))
  160. echo $_POST['password1'];?>">
  161. Confirm Password: <input type="password" name="password2"
  162. value="<?php if (isset($_POST['password2']))
  163. echo $_POST['password2'];?>">
  164. </p><p>
  165. <input type="submit" value="Register"> </p>
  166. </form>
  167.  
  168. <?php include ('includes/footer.html');?>
  169.  
  170. </body>
  171. </html>
Add Comment
Please, Sign In to add comment