Advertisement
academ1c

Username & Password Validation

Jun 27th, 2013
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.00 KB | None | 0 0
  1. <?php
  2. // Declare some stuff.
  3. $username = "";
  4. $password = "";
  5.  
  6. // This is not required, but is encouraged to declare all variables.
  7. $errors = array();
  8.  
  9. // Username stuff.
  10. if(strlen($username) == 0)
  11. {
  12.     $errors[] = "Where is your username!?";
  13. }
  14. elseif(strlen($username) < 2 || strlen($username) > 32)
  15. {
  16.     $errors[] = "Yur username doesn't meet the requirements! It must not fall below 2 and must not exceed 32 characters.";
  17. }
  18.  
  19. // Password stuff.
  20. // The second check is pointless, however, it's just an example of what you can do.
  21. if(strlen($password) <= 0)
  22. {
  23.     $errors[] = "Where is duh password!?";
  24. }
  25. elseif(hash("sha512", $password) !== hash("sha512", $password))
  26. {
  27.     $errors[] = "Why don't duh passwords match!?";
  28. }
  29.  
  30. // Count the errors, if their is >= 1, list them here.
  31. if(count($errors) >= 1)
  32. {
  33.     echo "You have the following problems..";
  34.     echo "<ul>";
  35.     foreach($errors as $error)
  36.     {
  37.         echo "<li>" . $error . "</li>";
  38.     }
  39.     echo "</ul>";
  40. }
  41. else
  42. {
  43.     echo "Login has been accepted.";
  44. }
  45. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement