Advertisement
WujuBean

Register.php // Project Crazy

Jul 5th, 2018
341
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.04 KB | None | 0 0
  1. <?php
  2. // Include config file
  3. require_once 'config.php';
  4.  
  5. // Define variables and initialize with empty values
  6. $username = $password = $confirm_password = $email = $toon = "";
  7. $username_err = $password_err = $confirm_password_err = $email_err = $toon_err = "";
  8.  
  9. // Processing form data when form is submitted
  10. if($_SERVER["REQUEST_METHOD"] == "POST"){
  11.  
  12. // Validate username
  13. if(empty(trim($_POST["username"]))){
  14. $username_err = "Please enter a username.";
  15. } else{
  16. // Prepare a select statement
  17. $sql = "SELECT id FROM users WHERE username = ?";
  18.  
  19. if($stmt = mysqli_prepare($link, $sql)){
  20.  
  21. // Set parameters
  22. $param_username = $username;
  23.  
  24. // Bind variables to the prepared statement as parameters
  25. mysqli_stmt_bind_param($stmt, "s", $username);
  26.  
  27.  
  28. // Attempt to execute the prepared statement
  29. if(mysqli_stmt_execute($stmt)){
  30. /* store result */
  31. mysqli_stmt_store_result($stmt);
  32.  
  33. if(mysqli_stmt_num_rows($stmt) == 1){
  34. $username_err = "This username is already taken.";
  35. } else{
  36. $username = trim($_POST["username"]);
  37. }
  38. } else{
  39. echo "Oops! Something went wrong. Please try again later.";
  40. }
  41. }
  42.  
  43. // Close statement
  44. mysqli_stmt_close($stmt);
  45. }
  46.  
  47. // Validate password
  48. if(empty(trim($_POST['password']))){
  49. $password_err = "Please enter a password.";
  50. } elseif(strlen(trim($_POST['password'])) < 6){
  51. $password_err = "Password must have atleast 6 characters.";
  52. } else{
  53. $password = trim($_POST['password']);
  54. }
  55.  
  56. // Validate confirm password
  57. if(empty(trim($_POST["confirm_password"]))){
  58. $confirm_password_err = 'Please confirm password.';
  59. } else{
  60. $confirm_password = trim($_POST['confirm_password']);
  61. if($password != $confirm_password){
  62. $confirm_password_err = 'Password did not match.';
  63. }
  64. }
  65.  
  66. // Validate email
  67. if(empty(trim($_POST["email"]))){
  68. $email_err = "Please enter your email.";
  69. } else{
  70. // Prepare a select statement
  71. $sql = "SELECT id FROM users WHERE email = ?";
  72.  
  73. if($stmt = mysqli_prepare($link, $sql)){
  74. // Set parameters
  75. $param_email = $email;
  76.  
  77. // Bind variables to the prepared statement as parameters
  78. mysqli_stmt_bind_param($stmt, "s", $email);
  79.  
  80.  
  81.  
  82. // Attempt to execute the prepared statement
  83. if(mysqli_stmt_execute($stmt)){
  84. /* store result */
  85. mysqli_stmt_store_result($stmt);
  86.  
  87. if(mysqli_stmt_num_rows($stmt) == 1){
  88. $email_err = "This email is already taken.";
  89. } else{
  90. $email = trim($_POST["email"]);
  91. }
  92. } else{
  93. echo "Oops! Something went wrong. Please try again later.";
  94. }
  95. }
  96.  
  97. // Close statement
  98. mysqli_stmt_close($stmt);
  99. }
  100.  
  101. // Validate toon
  102. if(empty(trim($_POST['toon']))){
  103. $toon_err = "Please enter a toon name.";
  104. } else{
  105. $toon = trim($_POST['toon']);
  106. }
  107. // Check input errors before inserting in database
  108. if(empty($username_err) && empty($password_err) && empty($confirm_password_err) && empty($email_err) && empty($toon_err)){
  109.  
  110. // Prepare an insert statement
  111. $sql = "INSERT INTO users (username, password, email, toon) VALUES (?, ?, ?, ?)";
  112.  
  113. if($stmt = mysqli_prepare($link, $sql)){
  114. // Set parameters
  115. $param_username = $username;
  116. $param_password = password_hash($password, PASSWORD_DEFAULT); // Creates a password hash
  117. $param_email = $email;
  118. $param_toon = $toon;
  119.  
  120. // Bind variables to the prepared statement as parameters
  121. mysqli_stmt_bind_param($stmt, "ss", $username, $password, $email, $toon);
  122.  
  123. // Attempt to execute the prepared statement
  124. if(mysqli_stmt_execute($stmt)){
  125. // Redirect to login page
  126. header("location: login.php");
  127. } else{
  128. echo "Something went wrong. Please try again later.";
  129. }
  130. }
  131.  
  132. // Close statement
  133. mysqli_stmt_close($stmt);
  134. }
  135.  
  136. // Close connection
  137. mysqli_close($link);
  138. }
  139. ?>
  140.  
  141. <!DOCTYPE html>
  142. <html lang="en">
  143. <head>
  144. <meta charset="UTF-8">
  145. <title>Sign Up</title>
  146. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.css">
  147. <style type="text/css">
  148. body{ font: 14px sans-serif; }
  149. .wrapper{ width: 350px; padding: 20px; }
  150. </style>
  151. </head>
  152. <body>
  153. <div class="wrapper">
  154. <h2>Sign Up</h2>
  155. <p>Please fill this form to create an account.</p>
  156. <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
  157. <div class="form-group <?php echo (!empty($username_err)) ? 'has-error' : ''; ?>">
  158. <label>Username</label>
  159. <input type="text" name="username"class="form-control" value="<?php echo $username; ?>">
  160. <span class="help-block"><?php echo $username_err; ?></span>
  161. </div>
  162. <div class="form-group <?php echo (!empty($password_err)) ? 'has-error' : ''; ?>">
  163. <label>Password</label>
  164. <input type="password" name="password" class="form-control" value="<?php echo $password; ?>">
  165. <span class="help-block"><?php echo $password_err; ?></span>
  166. </div>
  167. <div class="form-group <?php echo (!empty($confirm_password_err)) ? 'has-error' : ''; ?>">
  168. <label>Confirm Password</label>
  169. <input type="password" name="confirm_password" class="form-control" value="<?php echo $confirm_password; ?>">
  170. <span class="help-block"><?php echo $confirm_password_err; ?></span>
  171. </div>
  172. <div class="form-group <?php echo (!empty($email_err)) ? 'has-error' : ''; ?>">
  173. <label>Email</label>
  174. <input type="email" name="email" class="form-control" value="<?php echo $email; ?>">
  175. <span class="help-block"><?php echo $email_err; ?></span>
  176. </div>
  177. <div class="form-group <?php echo (!empty($toon_err)) ? 'has-error' : ''; ?>">
  178. <label>Toon</label>
  179. <input type="text" name="toon" class="form-control" value="<?php echo $toon; ?>">
  180. <span class="help-block"><?php echo $toon_err; ?></span>
  181. </div>
  182. <div class="form-group">
  183. <input type="submit" class="btn btn-primary" value="Submit">
  184. <input type="reset" class="btn btn-default" value="Reset">
  185. </div>
  186. <p>Already have an account? <a href="login.php">Login here</a>.</p>
  187. </form>
  188. </div>
  189. </body>
  190. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement