PHP Write login user name and password authenticator

Source: Internet
Author: User
Tags getmessage php write
  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 ', ' username cannot be less than 3 characters long. ');
  19. } elseif (Strlen ($username) >30) {
  20. $errors [] = new Error (' username ', ' username cannot be longer than 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);

  31. if ($user) {

  32. if (! ( $user->getpassword () = = SHA1 ($user->getsalt (). $password))) {
  33. $errors [] = new error (' Password ', ' username or password is wrong. ');
  34. }
  35. } else {
  36. $errors [] = new Error (' username ', ' username does not exist. ');
  37. }
  38. }
  39. return $errors;
  40. }
  41. }
  42. ?>

Copy Code

Error is a class that you write yourself:

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

Copy Code

2. Call Authenticator for validation

  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 and 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
  • 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.