欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

php写的登录时用户名与密码验证器

程序员文章站 2022-04-04 22:29:25
...
  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', '用户名不能为空。');
  17. } elseif (strlen($username) $errors[] = new Error('username', '用户名长度不能小于3个字符。');
  18. } elseif (strlen($username)>30) {
  19. $errors[] = new Error('username', '用户名长度不能超过30个字符。');
  20. } elseif (!preg_match('/^[A-Za-z]+$/',substr($username, 0, 1))) {
  21. $errors[] = new Error('username', '用户名必须以字母开头。');
  22. } elseif (!preg_match('/^[A-Za-z0-9_]+$/', $username)) {
  23. $errors[] = new Error('username', '用户名只能是字母、数字以及下划线( _ )的组合。');
  24. } elseif (!trim($password)) {
  25. $errors[] = new Error('password', '密码不能为空。');
  26. } else {
  27. // check whether use exists or not
  28. $dao = new UserDao();
  29. $user = $dao->findByName($username);
  30. if ($user) {

  31. if (!($user->getPassword() == sha1($user->getSalt() . $password))) {
  32. $errors[] = new Error('password', '用户名或密码错误。');
  33. }
  34. } else {
  35. $errors[] = new Error('username', '用户名不存在。');
  36. }
  37. }
  38. return $errors;
  39. }
  40. }
  41. ?>
复制代码

Error是自己写的一个类:

  1. /**

  2. * Validation error.
  3. */
  4. final class Error {
  5. private $source;

  6. private $message;
  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. ?>
复制代码

2、调用验证器进行验证

  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('登录成功!');
  20. Utils::redirect('welcome');
  21. }
  22. foreach ($errors as $e) {
  23. $msg .= $e->getMessage()."
    ";
  24. }
  25. ?>
复制代码