Guest User

Untitled

a guest
Apr 24th, 2018
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.19 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 = "";
  7. $username_err = $password_err = $confirm_password_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. // Bind variables to the prepared statement as parameters
  21. mysqli_stmt_bind_param($stmt, "s", $param_username);
  22.  
  23. // Set parameters
  24. $param_username = trim($_POST["username"]);
  25.  
  26. // Attempt to execute the prepared statement
  27. if(mysqli_stmt_execute($stmt)){
  28. /* store result */
  29. mysqli_stmt_store_result($stmt);
  30.  
  31. if(mysqli_stmt_num_rows($stmt) == 1){
  32. $username_err = "This username is already taken.";
  33. } else{
  34. $username = trim($_POST["username"]);
  35. }
  36. } else{
  37. echo "Oops! Something went wrong. Please try again later.";
  38. }
  39. }
  40.  
  41. // Close statement
  42. mysqli_stmt_close($stmt);
  43. }
  44.  
  45. // Validate password
  46. if(empty(trim($_POST['password']))){
  47. $password_err = "Please enter a password.";
  48. } elseif(strlen(trim($_POST['password'])) < 6){
  49. $password_err = "Password must have atleast 6 characters.";
  50. } else{
  51. $password = trim($_POST['password']);
  52. }
  53.  
  54. // Validate confirm password
  55. if(empty(trim($_POST["confirm_password"]))){
  56. $confirm_password_err = 'Please confirm password.';
  57. } else{
  58. $confirm_password = trim($_POST['confirm_password']);
  59. if($password != $confirm_password){
  60. $confirm_password_err = 'Password did not match.';
  61. }
  62. }
  63.  
  64. // Check input errors before inserting in database
  65. if(empty($username_err) && empty($password_err) && empty($confirm_password_err)){
  66.  
  67. // Prepare an insert statement
  68. $sql = "INSERT INTO users (username, password) VALUES (?, ?)";
  69.  
  70. if($stmt = mysqli_prepare($link, $sql)){
  71. // Bind variables to the prepared statement as parameters
  72. mysqli_stmt_bind_param($stmt, "ss", $param_username, $param_password);
  73.  
  74. // Set parameters
  75. $param_username = $username;
  76. $param_password = password_hash($password, PASSWORD_DEFAULT); // Creates a password hash
  77.  
  78. // Attempt to execute the prepared statement
  79. if(mysqli_stmt_execute($stmt)){
  80. // Redirect to login page
  81. header("location: login.php");
  82. } else{
  83. echo "Something went wrong. Please try again later.";
  84. }
  85. }
  86.  
  87. // Close statement
  88. mysqli_stmt_close($stmt);
  89. }
  90.  
  91. // Close connection
  92. mysqli_close($link);
  93. }
  94. ?>
  95.  
  96. <!DOCTYPE html>
  97. <html lang="en">
  98. <head>
  99. <meta charset="UTF-8">
  100. <title>Sign Up</title>
  101. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.css">
  102. <style type="text/css">
  103. body{ font: 14px sans-serif; }
  104. .wrapper{ width: 350px; padding: 20px; }
  105. </style>
  106. </head>
  107. <body>
  108. <div class="wrapper">
  109. <h2>Sign Up</h2>
  110. <p>Please fill this form to create an account.</p>
  111. <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
  112. <div class="form-group <?php echo (!empty($username_err)) ? 'has-error' : ''; ?>">
  113. <label>Username</label>
  114. <input type="text" name="username"class="form-control" value="<?php echo $username; ?>">
  115. <span class="help-block"><?php echo $username_err; ?></span>
  116. </div>
  117. <div class="form-group <?php echo (!empty($password_err)) ? 'has-error' : ''; ?>">
  118. <label>Password</label>
  119. <input type="password" name="password" class="form-control" value="<?php echo $password; ?>">
  120. <span class="help-block"><?php echo $password_err; ?></span>
  121. </div>
  122. <div class="form-group <?php echo (!empty($confirm_password_err)) ? 'has-error' : ''; ?>">
  123. <label>Confirm Password</label>
  124. <input type="password" name="confirm_password" class="form-control" value="<?php echo $confirm_password; ?>">
  125. <span class="help-block"><?php echo $confirm_password_err; ?></span>
  126. </div>
  127. <div class="form-group">
  128. <input type="submit" class="btn btn-primary" value="Submit">
  129. <input type="reset" class="btn btn-default" value="Reset">
  130. </div>
  131. <p>Already have an account? <a href="login.php">Login here</a>.</p>
  132. </form>
  133. </div>
  134. </body>
  135. </html>
Add Comment
Please, Sign In to add comment