Advertisement
Guest User

Untitled

a guest
Jan 11th, 2018
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.22 KB | None | 0 0
  1. <?php
  2. // Include config file
  3. if(isset($_POST['register'])){
  4.  
  5. require_once 'config.php';
  6.  
  7. // Define variables and initialize with empty values
  8. $username = $password = $confirm_password = "";
  9. $username_err = $password_err = $confirm_password_err = "";
  10.  
  11. // Processing form data when form is submitted
  12. if($_SERVER["REQUEST_METHOD"] == "POST"){
  13.  
  14. // Validate username
  15. if(empty(trim($_POST["username"]))){
  16. $username_err = "Please enter a username.";
  17. } else{
  18. // Prepare a select statement
  19. $sql = "SELECT id FROM users WHERE username = ?";
  20.  
  21. if($stmt = mysqli_prepare($link, $sql)){
  22. // Bind variables to the prepared statement as parameters
  23. mysqli_stmt_bind_param($stmt, "s", $param_username);
  24.  
  25. // Set parameters
  26. $param_username = trim($_POST["username"]);
  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. $uppercase = preg_match('@[A-Z]@', $_POST['password']);
  49. $lowercase = preg_match('@[a-z]@', $_POST['password']);
  50. $number = preg_match('@[0-9]@', $_POST['password']);
  51. if(empty(trim($_POST['password']))){
  52. $password_err = "Please enter a password.";
  53. } elseif(!$uppercase || !$lowercase || !$number || strlen(trim($_POST['password'])) < 8){
  54. $password_err = "Password must have atleast 8 characters, a uppercase letter and lowercase letter.";
  55. } else{
  56. $password = trim($_POST['password']);
  57. }
  58.  
  59. // Validate confirm password
  60. if(empty(trim($_POST["confirm_password"]))){
  61. $confirm_password_err = 'Please confirm password.';
  62. } else{
  63. $confirm_password = trim($_POST['confirm_password']);
  64. if($password != $confirm_password){
  65. $confirm_password_err = 'Password did not match.';
  66. }
  67. }
  68.  
  69. // Check input errors before inserting in database
  70. if(empty($username_err) && empty($password_err) && empty($confirm_password_err)){
  71.  
  72. // Prepare an insert statement
  73. $sql = "INSERT INTO users (username, password) VALUES (?, ?)";
  74.  
  75. if($stmt = mysqli_prepare($link, $sql)){
  76. // Bind variables to the prepared statement as parameters
  77. mysqli_stmt_bind_param($stmt, "ss", $param_username, $param_password);
  78.  
  79. // Set parameters
  80. $param_username = $username;
  81. $param_password = password_hash($password, PASSWORD_DEFAULT); // Creates a password hash
  82.  
  83. // Attempt to execute the prepared statement
  84. if(mysqli_stmt_execute($stmt)){
  85. // Redirect to login page
  86. header("location: login.php");
  87. } else{
  88. echo "Something went wrong. Please try again later.";
  89. }
  90. }
  91.  
  92. // Close statement
  93. mysqli_stmt_close($stmt);
  94. }
  95.  
  96. // Close connection
  97. mysqli_close($link);
  98. }
  99. }
  100. ?>
  101.  
  102. <!DOCTYPE html>
  103. <html lang="en">
  104. <head>
  105. <meta charset="UTF-8">
  106. <title>Sign Up</title>
  107. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.css">
  108. <style type="text/css">
  109. body{ font: 14px sans-serif; }
  110. .wrapper{ width: 350px; padding: 20px; }
  111. </style>
  112. </head>
  113. <body>
  114. <div class="wrapper">
  115. <h2>Sign Up</h2>
  116. <p>Please fill this form to create an account.</p>
  117. <form name = "register" id = "register" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
  118. <div class="form-group <?php echo (!empty($username_err)) ? 'has-error' : ''; ?>">
  119. <label>Username:<sup>*</sup></label>
  120. <input type="text" name="username"class="form-control" value="<?php echo $username; ?>">
  121. <span class="help-block"><?php echo $username_err; ?></span>
  122. </div>
  123. <div class="form-group <?php echo (!empty($password_err)) ? 'has-error' : ''; ?>">
  124. <label>Password:<sup>*</sup></label>
  125. <input type="password" name="password" class="form-control" value="<?php echo $password; ?>">
  126. <span class="help-block"><?php echo $password_err; ?></span>
  127. </div>
  128. <div class="form-group <?php echo (!empty($confirm_password_err)) ? 'has-error' : ''; ?>">
  129. <label>Confirm Password:<sup>*</sup></label>
  130. <input type="password" name="confirm_password" class="form-control" value="<?php echo $confirm_password; ?>">
  131. <span class="help-block"><?php echo $confirm_password_err; ?></span>
  132. </div>
  133. <div class="form-group">
  134. <input type="submit" name="register" class="btn btn-primary" value="Submit">
  135. <input type="reset" class="btn btn-default" value="Reset">
  136. </div>
  137. <p><a href="login.php">Login here</a>.</p>
  138. </form>
  139. <form action="createTable.php" method="get">
  140. <input type="submit" class="btn btn-primary" value="Create Table">
  141. </form>
  142. </div>
  143. </body>
  144. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement