Home  >  Article  >  Backend Development  >  Login username and password validator written in PHP

Login username and password validator written in PHP

WBOY
WBOYOriginal
2016-07-25 09:07:291241browse
  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 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', 'Username cannot be empty.');
  17. } elseif (strlen($username)<3) {
  18. $errors[] = new Error( 'username', 'The username cannot be less than 3 characters long');
  19. } elseif (strlen($username)>30) {
  20. $errors[] = new Error('username', 'The username cannot be longer than 3 characters. 30 characters. ');
  21. } elseif (!preg_match('/^[A-Za-z]+$/',substr($username, 0, 1))) {
  22. $errors[] = new Error( 'username', 'Username must start with a letter. ');
  23. } elseif (!preg_match('/^[A-Za-z0-9_]+$/', $username)) {
  24. $errors[] = new Error('username', 'Username can only be a combination of letters, numbers and underscores (_).');
  25. } elseif (!trim($password)) {
  26. $errors[] = new Error('password ', 'Password cannot be empty. ');
  27. } else {
  28. // check whether use exists or not
  29. $dao = new UserDao();
  30. $user = $dao->findByName($username);< /p>
  31. if ($user) {

  32. if (!($user->getPassword() == sha1($user->getSalt() . $password))) {
  33. $errors[ ] = new Error('password', 'Wrong username or password. ');
  34. }
  35. } else {
  36. $errors[] = new Error('username', 'Username does not exist.');
  37. }
  38. }
  39. return $errors;
  40. }
  41. }
  42. ?>< /p>
Copy code

Error is a class written by myself:

  1. /**

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

  5. private $source;

  6. private $message;< /p>
  7. /**
  8. * Create new error.
  9. * @param mixed $source 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. ?>

Copy the code

2. Call the validator to proceed verify

  1. $username = null;
  2. $password = null;

  3. $msg = "";

  4. < ;p>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. ?>

Copy code


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn