Advertisement
Guest User

Untitled

a guest
Jan 19th, 2017
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 7.51 KB | None | 0 0
  1. <html>
  2. <head>
  3. <title>Test Register Page</title>
  4. <meta name="viewport" content="width=device-width, initial-scale=1">
  5. <link rel="stylesheet" href="css/register.css" />
  6. <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
  7. <link rel="stylesheet" href="css/index.css" />
  8. <link rel="stylesheet" href="css/styles.css" />
  9. <link href='https://fonts.googleapis.com/css?family=Raleway:400,500,300,600' rel='stylesheet' type='text/css'>
  10. </head>
  11. <?php
  12. //This line creates the function test_input using the parameter $data
  13. function test_input($data) {
  14.  
  15. //This line removes all whitespace from the data parsed in
  16. $data = trim($data);
  17.  
  18. //This line removes all backslashes from the data
  19. $data = stripslashes($data);
  20.  
  21. //This line replaces all special characters with HTML escaped code to prevent cross-site scripting
  22. $data = htmlspecialchars($data);
  23.  
  24. //This line returns the resulting data to the above code
  25. return $data;
  26.  
  27. }
  28. include_once "navBar.php";
  29. //These lines create all of the variables and sets them to empty values
  30. $emailErr = $firstNErr = $surnErr = $phoneErr = $passErr = $confPassErr = "";
  31.  
  32. $email = $firstN = $surn = $phone = $pass = "";
  33. $error = 0;
  34.  
  35. //These lines ceate and assign the variables required to connect to the SQL database
  36.  
  37. $servername = "127.0.0.1";
  38. $username = "root";
  39. $password = "";
  40. $dbname = "accounts";
  41.  
  42. //These lines try to connect to the database with the assigned variables above
  43. $conn = new mysqli($servername, $username, $password, $dbname);
  44.  
  45. //This if statement runs the indented code below when the user clicks the register button
  46. if ($_SERVER["REQUEST_METHOD"] == "POST") {
  47.  
  48. //If the connection to the database fails, an error message is displayed to the user
  49. if ($conn->connect_error) {
  50.  
  51. die("Connection failed: " . $conn->connect_error . "/nPlease try again. If it still doesn't work, copy the error message and send it to cjbrennan2701@gmail.com");
  52.  
  53. }
  54.  
  55. //This line checks whether the user has entered anything in the email box
  56. if (empty($_POST["email"])) {
  57.  
  58. //If they haven't, a suitable error message is displayed to the user
  59. //Also, the value of error becomes 1
  60. $emailErr = "E-mail required";
  61. $error = 1;
  62. } else if (strpos($_POST["email"],'@') === FALSE && $error != 1){
  63.     $emailErr = "Please enter a valid email address";
  64. } else {
  65.  
  66. //0therwise, the user's input is stripped of any whitespace or backslashes
  67. //Any special characters in the input are converted to HTML escaped code to prevent malicious attacks
  68. $email = test_input($_POST["email"]);
  69. }
  70.  
  71. //The first name box is checked to see whether it is empty
  72. if (empty($_POST["firstN"])) {
  73. //If it is, the user is told to enter a first name and the value of error becomes 1
  74.  
  75. $firstNErr = "First name required";
  76. $error = 1;
  77. } else {
  78.  
  79. //If not, the function test_input removes whitespace and backslashes from the imput
  80. //and special characters are replaced by HTML escaped code
  81. $firstN = test_input($_POST["firstN"]);
  82. }
  83.  
  84. //The surname box is checkked to see whether it is empty
  85. if (empty($_POST["surn"])) {
  86.  
  87. //If it is empty the user is informed and the value of error is changed to 1
  88. $surnErr = "Surname required";
  89. $error = 1;
  90. } else {
  91.  
  92. //If it isn't empty, test_input removes backslashes and whitespace
  93. //as well as replaces any special characters with HTML escaped code
  94. $surn = test_input($_POST["surn"]);
  95. }
  96.  
  97. //The user's input for their phone is then checked
  98. if (empty($_POST["phone"])) {
  99.  
  100. //If it is empty, then nothing is stored
  101.  
  102. $phone = "";
  103. } elseif(strlen($_POST["phone"] === 11) && is_numeric($_POST["phone"])){
  104.  
  105. //If the user's input only contains numbers and is 11 characters long
  106. //it is stored as $phone
  107. $phone = test_input($_POST["phone"]);
  108.  
  109. //If any other format of input is detected, an error is produced
  110. } else {
  111. $phoneErr = "Please enter a valid phone number";
  112.  
  113. }
  114. //The user's input for their password is then checked
  115. //If the box is empty, an error message is produced and error becomes 1
  116. if (empty($_POST["password"])) {
  117. $passErr = "Password required";
  118. $error = 1;
  119.  
  120. //0therwise, the user's password has whitespace and backslashes removed and is then stored
  121. } else {
  122. $pass = test_input($_POST["password"]);
  123. }
  124.  
  125. //Finally, the user's input for confirming their password is checked
  126. //if it is empty, a suitable error message is produced and error is changed to 1
  127. if (empty($_POST["confPass"])) {
  128. $confPassErr = "Password confirmation required";
  129. $error = 1;
  130.  
  131. }
  132.  
  133. //A1so, if the user's input for their password and password confirmation don't match,
  134. //a message on the page tells the user and error is changed to 1
  135. elseif ($_POST["confPass"] != $_POST["password"] && $passErr === "") {
  136. $confPassErr = "Passwords don't match";
  137. $error = 1;
  138.  
  139. }
  140.  
  141. //The variable $sql is created which is used to store the query that will be sent to the database
  142. $sql = "INSERT INTO testTable (email, firstN, surn, phone, psswrd)
  143. VALUES ('$email', '$firstN', '$surn', '$phone', '$pass')";
  144.  
  145. //If no errors were found in the user's inputs, then the data is sent to the database to be stored
  146. if ($error === 0){
  147. if ($conn->query($sql) === TRUE) {
  148.  
  149. //If the connection to the database fails, an error saying what went wrong is produced
  150. } else {
  151. echo "Error: " . $sql . "
  152. <br>" . $conn->error;
  153.  
  154. }
  155. }
  156.  
  157. //This line ends the connection to the database
  158. $conn->close();
  159.  
  160.  
  161.  
  162. }
  163. ?>
  164.  
  165. <body>
  166. <div class="container-fluid">
  167.   <div class="row">
  168.     <div class="col-md-3"></div>
  169.     <div class="col-xs-12 col-md-6">
  170.       <!-- This line creates a form where when the user clicks submit, runs the PHP code above -->
  171.       <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
  172. <h5 style="color:#136002; font-size:16px;">Please fill out the information below<br>
  173.           Fields marked by an asterisk * are required</h5>
  174.         <div class="row">
  175.           <div id="text" class="col-xs-6">
  176.             <!-- These lines provide an input box for the user to type their email address as well as a space to display any errors -->
  177.             <p>E-mail: </p>
  178.             <br>
  179.             <p>First name: </p>
  180.             <br>
  181.             <p>Surname: </p>
  182.             <br>
  183.             <p>Phone number: </p>
  184.             <br>
  185.             <p>Password: </p>
  186.             <br>
  187.             <p>Confirm password: </p>
  188.           </div>
  189.           <div id="input" class="col-xs-6">
  190.             <p>
  191.               <input type="text" name="email" id="email">
  192.               <span class="error">* <?php echo $emailErr;?></span></p>
  193.             <p>
  194.               <input type="text" name="firstN" id="firstN">
  195.               <span class="error">* <?php echo $firstNErr;?></span></p>
  196.             <p>
  197.               <input type="text" name="surn" id="surn">
  198.               <span class="error">* <?php echo $surnErr;?></span></p>
  199.             <p>
  200.               <input type="text" name="phone" id="phone">
  201.               <span class="error"> <?php echo $phoneErr;?></span></p>
  202.             <p>
  203.               <input type="text" name="password" id="password">
  204.               <span class="error">* <?php echo $passErr;?></span></p>
  205.             <p style="margin-bottom:6px;">
  206.               <input type="text" name="confPass">
  207.               <span class="error">* <?php echo $confPassErr;?></span></p>
  208.           </div>
  209.         </div>
  210.         <div class="row">
  211.           <div id="btnReg" class="col-sm-12">
  212.             <button type="submit" name="submit">Register</button>
  213.           </div>
  214.         </div>
  215. </form>
  216.     </div>
  217.     <div class="col-sm-2 col-md-3"></div>
  218.   </div>
  219. </div>
  220. </body>
  221. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement