Username and password validators for php login

Source: Internet
Author: User
Username and password validators for php login

  1. /**

  2. * Validator for Login.
  3. */
  4. Final class LoginValidator {

  5. Private function _ construct (){

  6. }

  7. /**

  8. * Validate the given username and password.
  9. * @ Param $ username and $ password to be validated
  10. * @ Return array of {@ link Error} s
  11. */
  12. Public static function validate ($ username, $ password ){
  13. $ Errors = array ();
  14. $ Username = trim ($ username );
  15. If (! $ Username ){
  16. $ Errors [] = new Error ('username', 'user name cannot be blank. ');
  17. } Elseif (strlen ($ username) <3 ){
  18. $ Errors [] = new Error ('username', 'user name length cannot be less than 3 characters. ');
  19. } Elseif (strlen ($ username)> 30 ){
  20. $ Errors [] = new Error ('username', 'the username length cannot exceed 30 characters. ');
  21. } Elseif (! Preg_match ('/^ [A-Za-z] + $/', substr ($ username, 0, 1 ))){
  22. $ Errors [] = new Error ('username', 'the user name must start with a letter. ');
  23. } Elseif (! Preg_match ('/^ [A-Za-z0-9 _] + $/', $ username )){
  24. $ Errors [] = new Error ('username', 'user name can only be a combination of letters, numbers, and underscores. ');
  25. } Elseif (! Trim ($ password )){
  26. $ Errors [] = new Error ('password', 'the password cannot be blank. ');
  27. } Else {
  28. // Check whether use exists or not
  29. $ Dao = new UserDao ();
  30. $ User = $ dao-> findByName ($ username );

  31. If ($ user ){

  32. If (! ($ User-> getPassword () = sha1 ($ user-> getSalt (). $ password ))){
  33. $ Errors [] = new Error ('password', 'the user name or password is incorrect. ');
  34. }
  35. } Else {
  36. $ Errors [] = new Error ('username', 'user name does not exist. ');
  37. }
  38. }
  39. Return $ errors;
  40. }
  41. }
  42. ?>

Error is a self-written class:

  1. /**

  2. * Validation error.
  3. */
  4. Final class Error {

  5. Private $ source;

  6. Private $ message;

  7. /**
  8. * Create new error.
  9. * @ Param mixed $ source of the error
  10. * @ Param string $ message error message
  11. */
  12. Function _ construct ($ source, $ message ){
  13. $ This-> source = $ source;
  14. $ This-> message = $ message;
  15. }

  16. /**

  17. * Get source of the error.
  18. * @ Return mixed source of the error
  19. */
  20. Public function getSource (){
  21. Return $ this-> source;
  22. }

  23. /**

  24. * Get error message.
  25. * @ Return string error message
  26. */
  27. Public function getMessage (){
  28. Return $ this-> message;
  29. }
  30. }
  31. ?>

2. call the validators for verification.

  1. $ Username = null;

  2. $ Password = null;

  3. $ Msg = "";

  4. If (isset ($ _ POST ['username']) & isset ($ _ POST ['password']) {

  5. $ Username = addslashes (trim (stripslashes ($ _ POST ['username']);
  6. $ Password = addslashes (trim (stripslashes ($ _ POST ['password']);
  7. // Validate
  8. $ Errors = LoginValidator: validate ($ username, $ password );
  9. If (empty ($ errors )){
  10. // Save the latest ip or login time into database, then processing page forwarding
  11. $ Dao = new UserDao ();
  12. $ User = $ dao-> findByName ($ username );
  13. $ Last_login_ip = Utils: getIpAddress ();
  14. $ User-> setLastLoginIp ($ last_login_ip );
  15. $ Now = new DateTime ();
  16. $ User-> setLastLoginTime ($ now );
  17. $ Dao-> save ($ user );
  18. UserLogin: setUserInfo ($ user );
  19. Flash: addFlash ('login successful! ');
  20. Utils: redirect ('Welcome ');
  21. }
  22. Foreach ($ errors as $ e ){
  23. $ Msg. = $ e-> getMessage ()."
    ";
  24. }
  25. ?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.