Advertisement
Guest User

Untitled

a guest
Jan 19th, 2016
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.35 KB | None | 0 0
  1. <?php
  2. include_once "config.php";
  3.  
  4. $error_msg = "";
  5.  
  6. if (isset($_POST['username'], $_POST['email'], $_POST['p'])) {
  7. // Sanitize and validate the data passed in
  8. $username = filter_input(INPUT_POST, 'username', FILTER_SANITIZE_STRING);
  9. $email = filter_input(INPUT_POST, 'email', FILTER_SANITIZE_EMAIL);
  10. $email = filter_var($email, FILTER_VALIDATE_EMAIL);
  11. if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
  12. // Not a valid email
  13. $error_msg .= '<p class="error">The email address you entered is not valid</p>';
  14. }
  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. If it's not, something really odd has happened
  20. $error_msg .= '<p class="error">Invalid password configuration.</p>';
  21. }
  22.  
  23. $print_r = "SELECT id FROM users WHERE email = ? LIMIT 1";
  24. $stmt = $mysqli->prepare($print_r);
  25.  
  26. if ($stmt) {
  27. $stmt->bind_param('s', $email);
  28. $stmt->execute();
  29. $stmt->store_result();
  30.  
  31. if ($stmt->num_rows == 1) {
  32. // A user with this email address already exists
  33. $error_msg .= '<p class="error">A user with this email address already exists.</p>';
  34. }
  35. } else {
  36. $error_msg .= '<p class="error">Database error</p>';
  37. }
  38.  
  39. // We'll also have to account for the situation where the user doesn't have
  40. // rights to do registration, by checking what type of user is attempting to perform the operation.
  41.  
  42. if (empty($error_msg)) {
  43. // Create a random salt
  44. $random_salt = hash('sha512', uniqid(openssl_random_pseudo_bytes(16), TRUE));
  45.  
  46. // Create salted password
  47. $password = hash('sha512', $password . $random_salt);
  48.  
  49. // Insert the new user into the database
  50. if ($insert_stmt = $mysqli->prepare("INSERT INTO users (username, email, password, salt) VALUES (?, ?, ?, ?)")) {
  51. $insert_stmt->bind_param('ssss', $username, $email, $password, $random_salt);
  52. // Execute the prepared query.
  53. if (! $insert_stmt->execute()) {
  54. header('Location: ../error.php?err=Registration failure: INSERT');
  55. exit();
  56. }
  57. }
  58. header('Location: ./register.php');
  59. exit();
  60. }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement