Advertisement
CSD_Fyre

Untitled

May 21st, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.92 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($sql)){
  20. // Bind variables to the prepared statement as parameters
  21. $stmt->bind_param("s", $param_username);
  22.  
  23. // Set parameters
  24. $param_username = trim($_POST["username"]);
  25.  
  26. // Attempt to execute the prepared statement
  27. if($stmt->execute()){
  28. // store result
  29. $stmt->store_result();
  30.  
  31. if($stmt->num_rows == 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. $stmt->close();
  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(empty($password_err) && ($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($sql)){
  71. // Bind variables to the prepared statement as parameters
  72. $stmt->bind_param("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($stmt->execute()){
  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. $stmt->close();
  89. }
  90.  
  91. // Close connection
  92. $mysqli->close();
  93. }
  94. ?>
  95.  
  96. <html>
  97. <head>
  98. <link rel="icon" href="https://github.com/jchernin4/HWIDS/raw/master/Infinium%20Logo.png">
  99. <link href="style.css" rel="stylesheet" type="text/css" />
  100. <meta charset="utf-8">
  101. <meta name="viewport" content="width=device-width">
  102. </head>
  103. <body>
  104. <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post" style="border:1px solid #ccc">
  105. <div class="container">
  106. <h1>Sign Up</h1>
  107. <p>Please fill in this form to create an account.</p>
  108. <hr>
  109.  
  110. <label for="email"><b>Email</b></label>
  111. <input type="email" placeholder="Enter Email" name="email" required>
  112.  
  113. <label for="username"><b>Username</b></label>
  114. <input type="text" placeholder="Enter Username" name="username" required>
  115.  
  116. <label for="password"><b>Password</b></label>
  117. <input type="password" placeholder="Enter Password" name="password" required>
  118.  
  119. <label for="confirm_password"><b>Confirm Password</b></label>
  120. <input type="password" placeholder="Confirm Password" name="confirm_password" required>
  121.  
  122. <label>
  123. <input type="checkbox" checked="checked" name="remember" style="margin-bottom:15px"> Remember me
  124. </label>
  125.  
  126. <p>By creating an account you agree to our <a href="#" style="color:dodgerblue">Terms & Privacy</a>.</p>
  127.  
  128. <div class="clearfix">
  129. <button type="button" class="cancelbtn">Cancel</button>
  130. <button type="submit" class="signupbtn">Sign Up</button>
  131. </div>
  132. </div>
  133. </form>
  134. </body>
  135. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement