Advertisement
Guest User

Untitled

a guest
Feb 7th, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.18 KB | None | 0 0
  1. if(isset($_POST['name']) && $_POST['name'] != "") // Checking if there is username input
  2. {
  3. if(strlen ($_POST['name']) >= $usernameMinLength && strlen ($_POST['name']) <= $usernameMaxLength) // Check username length criteria
  4. {
  5. if(isset($_SESSION['username'])) // Dont allow register if user is already logged in
  6. {
  7. $globalError = "Already registered, please <b>Logout</b> to register a new <b>User</b>";
  8. }
  9. else // If user is NOT logged in , then allow register
  10. {
  11. if($_POST['name'] == clear_string($_POST['name'])) // Check if username contains special characters, if username = stripped username, then continue, otherwise give error
  12. {
  13. if(strlen ($_POST['password']) >= $passwordMinLength && strlen ($_POST['password']) <= $passwordMaxLength) // Check if password is long enough
  14. {
  15. { // Check if username exists
  16. $sql = "SELECT username FROM users";
  17. $result = $conn->query($sql);
  18. if ($result->num_rows > 0)
  19. {
  20. $usernameError = "Username is <b>Already</b> taken!";
  21. }
  22. }
  23. $hashPassword = hash('sha256', $_POST['password']); // Hash the input passoword
  24.  
  25. $sql = "INSERT INTO users (username, password)
  26. VALUES ('" . $_POST['name'] . "', '" . $hashPassword . "')"; // Put name and password in query
  27.  
  28. if ($conn->query($sql) === TRUE) // Test if query is successful
  29. {
  30. header("Location: login.php");
  31. }
  32. }
  33. else
  34. {
  35. $passwordError = "Password doesnt fit the <b>requrements</b>, must be " . $passwordMinLength . " to " . $passwordMaxLength . " characters <b>Long</b>";
  36. }
  37. }
  38. else // If username contains special chars
  39. {
  40. $usernameError = "Username contains special characters, try with " . clear_string($_POST['name']); // Throw username error
  41. }
  42. }
  43. }
  44. else
  45. {
  46. $usernameError = "Username doesnt fit the <b>requrements</b>, must be " . $usernameMinLength . " to " . $usernameMaxLength . " characters <b>Long</b>";
  47. }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement