Guest User

Untitled

a guest
Mar 16th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.28 KB | None | 0 0
  1. <?php
  2. include_once 'db_connect.php';
  3. include_once 'psl-config.php';
  4.  
  5. $error_msg = "";
  6.  
  7. if (isset($_POST['username'], $_POST['email'], $_POST['p'])) {
  8. // Sanitize and validate the data passed in
  9. $username = filter_input(INPUT_POST, 'username', FILTER_SANITIZE_STRING);
  10. $email = filter_input(INPUT_POST, 'email', FILTER_SANITIZE_EMAIL);
  11. $email = filter_var($email, FILTER_VALIDATE_EMAIL);
  12. if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
  13. // Not a valid email
  14. $error_msg .= '<p class="error">The email address you entered is not valid</p>';
  15. }
  16.  
  17. $password = filter_input(INPUT_POST, 'p', FILTER_SANITIZE_STRING);
  18. if (strlen($password) != 128) {
  19. // The hashed pwd should be 128 characters long.
  20. // If it's not, something really odd has happened
  21. $error_msg .= '<p class="error">Invalid password configuration.</p>';
  22. }
  23.  
  24. // Username validity and password validity have been checked client side.
  25. // This should should be adequate as nobody gains any advantage from
  26. // breaking these rules.
  27. //
  28.  
  29. $prep_stmt = "SELECT id FROM members WHERE email = ? LIMIT 1";
  30. $stmt = $mysqli->prepare($prep_stmt);
  31.  
  32. // check existing email
  33. if ($stmt) {
  34. $stmt->bind_param('s', $email);
  35. $stmt->execute();
  36. $stmt->store_result();
  37.  
  38. if ($stmt->num_rows == 1) {
  39. // A user with this email address already exists
  40. $error_msg .= '<p class="error">A user with this email address already exists.</p>';
  41. $stmt->close();
  42. }
  43. } else {
  44. $error_msg .= '<p class="error">Database error Line 39</p>';
  45. $stmt->close();
  46. }
  47.  
  48. // check existing username
  49. $prep_stmt = "SELECT id FROM members WHERE username = ? LIMIT 1";
  50. $stmt = $mysqli->prepare($prep_stmt);
  51.  
  52. if ($stmt) {
  53. $stmt->bind_param('s', $username);
  54. $stmt->execute();
  55. $stmt->store_result();
  56.  
  57. if ($stmt->num_rows == 1) {
  58. // A user with this username already exists
  59. $error_msg .= '<p class="error">A user with this username already exists</p>';
  60. $stmt->close();
  61. }
  62. } else {
  63. $error_msg .= '<p class="error">Database error line 55</p>';
  64. $stmt->close();
  65. }
  66.  
  67. // TODO:
  68. // We'll also have to account for the situation where the user doesn't have
  69. // rights to do registration, by checking what type of user is attempting to
  70. // perform the operation.
  71.  
  72. if (empty($error_msg)) {
  73.  
  74. // Create hashed password using the password_hash function.
  75. // This function salts it with a random salt and can be verified with
  76. // the password_verify function.
  77. $password = password_hash($password, PASSWORD_BCRYPT);
  78.  
  79. // Insert the new user into the database
  80. if ($insert_stmt = $mysqli->prepare("INSERT INTO members (username, email, password) VALUES (?, ?, ?)")) {
  81. $insert_stmt->bind_param('sss', $username, $email, $password);
  82. // Execute the prepared query.
  83. if (! $insert_stmt->execute()) {
  84. header('Location: ../error.php?err=Registration failure: INSERT');
  85. }
  86. }
  87. header('Location: ./register_success.php');
  88. }
  89. }
  90. ?>
Add Comment
Please, Sign In to add comment