Advertisement
Guest User

Validation.php

a guest
Aug 18th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.90 KB | None | 0 0
  1. <?php
  2. error_reporting(E_ALL);
  3. ini_set("display_errors", 1);
  4. // Include config file
  5.  
  6.  
  7. // Define variables and initialize with empty values
  8.  
  9. $username_err = $password_err = $confirm_password_err = $passwordConf_err = $email_err = $email_used_err = "";
  10.  
  11. $username = $password = $email = "";
  12. $param_password = $param_username =$param_email = "";
  13. $passwordConf = "";
  14.  
  15.  
  16.  
  17. // Processing form data when form is submitted
  18. if($_SERVER["REQUEST_METHOD"] == "POST"){
  19.  
  20. // Validate username
  21. if(empty(trim($_POST["username"]))){
  22. $username_err = "Please enter a username.";
  23. } else{
  24. require_once "db_conn.php";
  25. // Prepare a select statement
  26. $sql = "SELECT id FROM estiweb_db WHERE username = ?";
  27.  
  28. if($stmt = mysqli_prepare($link, $sql)){
  29. // Bind variables to the prepared statement as parameters
  30. mysqli_stmt_bind_param($stmt, "s", $param_username);
  31.  
  32. // Set parameters
  33. $param_username = trim($_POST["username"]);
  34.  
  35. // Attempt to execute the prepared statement
  36. if(mysqli_stmt_execute($stmt)){
  37. // store result
  38. mysqli_stmt_store_result($stmt);
  39.  
  40. if(mysqli_stmt_num_rows($stmt) == 1){
  41. $username_err = "This username is already taken.";
  42. } else{
  43. $username = trim($_POST["username"]);
  44. }
  45. } else{
  46. echo "Oops! Something went wrong. Please try again later.";
  47. }
  48. }
  49.  
  50. // Close statement
  51. mysqli_stmt_close($stmt);
  52. }
  53. //validate email
  54. if(empty(trim($_POST["email"]))){
  55. $email_err = "Please fill in the Email!";
  56. } else{
  57. require_once "db_conn.php";
  58. // Prepare a select statement
  59. $sql = "SELECT id FROM estiweb_db WHERE email = ?";
  60.  
  61. if($stmt = mysqli_prepare($link, $sql)){
  62. // Bind variables to the prepared statement as parameters
  63. mysqli_stmt_bind_param($stmt, "s", $param_email);
  64.  
  65. // Set parameters
  66. $param_email = trim($_POST["email"]);
  67.  
  68. // Attempt to execute the prepared statement
  69. if(mysqli_stmt_execute($stmt)){
  70. // store result
  71. mysqli_stmt_store_result($stmt);
  72.  
  73. if(mysqli_stmt_num_rows($stmt) == 1){
  74. $email_used_err = "This email is already in use!";
  75. } else{
  76. $email = trim($_POST["email"]);
  77. }
  78. } else{
  79. echo "Oops! Something went wrong. Please try again later.";
  80. }
  81.  
  82.  
  83. // Close statement
  84. mysqli_stmt_close($stmt);
  85. }
  86. // Validate password
  87. if(empty(trim($_POST["password"]))){
  88. $password_err = "Please enter a password.";
  89. } elseif(strlen(trim($_POST["password"])) < 6){
  90. $password_err = "Password must have atleast 6 characters.";
  91. } else{
  92. $password = trim($_POST["password"]);
  93. }
  94.  
  95. // Validate confirm password
  96. if(empty(trim($_POST["passwordConf"]))){
  97. $passwordConf_err = "Please confirm password.";
  98. } else{
  99. $passwordConf = trim($_POST["passwordConf"]);
  100. if(empty($password_err) && ($password != $passwordConf)){
  101. $confirm_password_err = "Password did not match.";
  102. }
  103. }
  104. /*
  105. echo $username_err;
  106. echo $password_err;
  107. echo $email_err;
  108. */
  109. // Check input errors before inserting in database
  110. if(empty($username_err) && empty($password_err) && empty($email_err) && empty($confirm_password_err) && empty($passwordConf_err)){
  111. require_once "db_conn.php";
  112. // Prepare an insert statement
  113. $sql = mysqli_query("INSERT INTO estiweb_db (username, email, password) VALUES (? ,?, ?)");
  114.  
  115. if($stmt = mysqli_prepare($link, $sql)){
  116. // Bind variables to the prepared statement as parameters
  117. mysqli_stmt_bind_param($stmt, "sss", $param_username, $param_email, $param_password);
  118.  
  119. // Set parameters
  120. $param_username = $username;
  121. $param_password = password_hash($password, PASSWORD_DEFAULT); // Creates a password hash
  122. $param_email = $email;
  123.  
  124. // Attempt to execute the prepared statement
  125.  
  126. if ($link->query($stmt) === TRUE) {
  127. //header("location: ../login.php")
  128. echo "New record created successfully";
  129. } else {
  130. echo "Error: " . $sql . "<br>" . $conn->error;
  131. }
  132. } else{
  133. echo "Something went wrong. Please try again later.";
  134. }
  135. }
  136.  
  137. // Close statement
  138. mysqli_stmt_close($stmt);
  139. }
  140.  
  141. // Close connection
  142. mysqli_close($link);
  143. }
  144. }
  145. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement