php寫的登入時使用者名稱與密碼驗證器

來源:互聯網
上載者:User
  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. ?>

複製代碼
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.