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

原创
2016-07-25 09:07:29 924浏览
  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)<3) {
  18. $errors[] = new Error('username', '用户名长度不能小于3个字符。');
  19. } elseif (strlen($username)>30) {
  20. $errors[] = new Error('username', '用户名长度不能超过30个字符。');
  21. } elseif (!preg_match('/^[A-Za-z]+$/',substr($username, 0, 1))) {
  22. $errors[] = new Error('username', '用户名必须以字母开头。');
  23. } elseif (!preg_match('/^[A-Za-z0-9_]+$/', $username)) {
  24. $errors[] = new Error('username', '用户名只能是字母、数字以及下划线( _ )的组合。');
  25. } elseif (!trim($password)) {
  26. $errors[] = new Error('password', '密码不能为空。');
  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', '用户名或密码错误。');
  34. }
  35. } else {
  36. $errors[] = new Error('username', '用户名不存在。');
  37. }
  38. }
  39. return $errors;
  40. }
  41. }
  42. ?>

复制代码

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. ?>

复制代码


声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
上一条:php和js实现转换文件大小为人性化可读的方式 下一条:有关php中autoload的机制详解

相关文章

查看更多