当前位置:Gxlcms > PHP教程 > php写的一个登录验证类

php写的一个登录验证类

时间:2021-07-01 10:21:17 帮助过:22人阅读

  1. final class UserLogin {

  2. public function __construct() {

  3. }
  4. public static function getUserInfo() {
  5. if (isset($_COOKIE["user_id"])&&$_COOKIE["user_id"]&&(trim($_COOKIE["user_id"])!="")) {
  6. if (isset($_SESSION["USER_INFO"]))
  7. return $_SESSION["USER_INFO"];
  8. $dao = new UserDao();
  9. $user = $dao->find($_COOKIE["user_id"]);
  10. if ($user) {
  11. $_SESSION["USER_INFO"] = $user;
  12. setcookie("docloud_sid", session_id(), time() + 36000);
  13. setcookie("user_id", $_COOKIE["user_id"], time() + 36000);
  14. if (array_key_exists("selected_prj_id", $_COOKIE))
  15. setcookie("selected_prj_id", $_COOKIE["selected_prj_id"], time() + 36000);
  16. if (array_key_exists("selected_class_id", $_COOKIE))
  17. setcookie("selected_class_id", $_COOKIE["selected_class_id"], time() + 36000);
  18. if (array_key_exists("selected_image_id", $_COOKIE))
  19. setcookie("selected_image_id", $_COOKIE["selected_image_id"], time() + 36000);
  20. if (array_key_exists("test_image_ids", $_COOKIE))
  21. setcookie("test_image_ids", $_COOKIE["test_image_ids"], time() + 36000);
  22. if (array_key_exists("upload_image_ids", $_COOKIE))
  23. setcookie("upload_image_ids", $_COOKIE["upload_image_ids"], time() + 36000);
  24. return $user;
  25. }
  26. }
  27. self::clearCookie();
  28. return null;
  29. }

  30. public static function setUserInfo($userInfo) {

  31. $_SESSION["USER_INFO"] = $userInfo;
  32. setcookie("docloud_sid", session_id(), time() + 36000);
  33. setcookie("user_id", $userInfo->getId(), time() + 36000);
  34. }

  35. public static function isLogin() {

  36. if (self::getUserInfo()) {
  37. return true;
  38. }
  39. return false;
  40. }

  41. public static function delUserInfo() {

  42. self::clearCookie();
  43. session_destroy();
  44. }
  45. private static function clearCookie() {
  46. setcookie("docloud_sid", "", time() - 36000);
  47. setcookie("user_id", "", time() - 36000);
  48. setcookie("selected_prj_id", "", time() - 36000);
  49. setcookie("selected_class_id", "", time() - 36000);
  50. setcookie("selected_image_id", "", time() - 36000);
  51. setcookie("test_image_ids", "", time() - 36000);
  52. setcookie("upload_image_ids", "", time() - 36000);
  53. }
  54. }

  55. /**

  56. * Validator for Login.
  57. */
  58. final class LoginValidator {
  59. private function __construct() {
  60. }

  61. /**

  62. * Validate the given username and password.
  63. * @param $username and $password to be validated
  64. * @return array array of {@link Error} s
  65. */
  66. public static function validate($username, $password) {
  67. $errors = array();
  68. $username = trim($username);
  69. if (!$username) {
  70. $errors[] = new Error('username', '用户名不能为空。');
  71. } elseif (strlen($username)<3) {
  72. $errors[] = new Error('username', '用户名长度不能小于3个字符。');
  73. } elseif (strlen($username)>30) {
  74. $errors[] = new Error('username', '用户名长度不能超过30个字符。');
  75. } elseif (!preg_match('/^[A-Za-z]+$/',substr($username, 0, 1))) {
  76. $errors[] = new Error('username', '用户名必须以字母开头。');
  77. } elseif (!preg_match('/^[A-Za-z0-9_]+$/', $username)) {
  78. $errors[] = new Error('username', '用户名只能是字母、数字以及下划线( _ )的组合。');
  79. } elseif (!trim($password)) {
  80. $errors[] = new Error('password', '密码不能为空。');
  81. } else {
  82. // check whether use exists or not
  83. $dao = new UserDao();
  84. $user = $dao->findByName($username);

  85. if ($user) {

  86. if (!($user->getPassword() == sha1($user->getSalt() . $password))) {
  87. $errors[] = new Error('password', '用户名或密码错误。');
  88. }
  89. } else {
  90. $errors[] = new Error('username', '用户名不存在。');
  91. }
  92. }
  93. return $errors;
  94. }
  95. }

  96. /**

  97. * Validation error.
  98. */
  99. final class Error {
  100. private $source;
  101. private $message;

  102. /**

  103. * Create new error.
  104. * @param mixed $source source of the error
  105. * @param string $message error message
  106. */
  107. function __construct($source, $message) {
  108. $this->source = $source;
  109. $this->message = $message;
  110. }

  111. /**

  112. * Get source of the error.
  113. * @return mixed source of the error
  114. */
  115. public function getSource() {
  116. return $this->source;
  117. }

  118. /**

  119. * Get error message.
  120. * @return string error message
  121. */
  122. public function getMessage() {
  123. return $this->message;
  124. }
  125. }

  126. // if logged in, logout 页面的跳转类在http://www.cnblogs.com/setsail/archive/2012/12/18/2823231.html 里这里不再重复书写

  127. if (UserLogin::isLogin() && $_COOKIE["user_id"]==1) {
  128. UserLogin::delUserInfo();
  129. }elseif (UserLogin::isLogin()){
  130. Utils::redirect('welcome');
  131. }

  132. $username = null;

  133. $password = null;
  134. $msg = "";

  135. if (isset($_POST['username']) && isset($_POST['password'])) {

  136. $username = addslashes(trim(stripslashes($_POST ['username'])));
  137. $password = addslashes(trim(stripslashes($_POST ['password'])));
  138. // validate
  139. $errors = LoginValidator::validate($username, $password);
  140. if (empty($errors)) {
  141. // save
  142. $dao = new UserDao();
  143. $user = $dao->findByName($username);
  144. $last_login_ip = Utils::getIpAddress();
  145. $user->setLastLoginIp($last_login_ip);
  146. $now = new DateTime();
  147. $user->setLastLoginTime($now);
  148. $dao->save($user);
  149. UserLogin::setUserInfo($user);
  150. Flash::addFlash('登录成功!');
  151. Utils::redirect('welcome');
  152. }
  153. foreach ($errors as $e) {
  154. $msg .= $e->getMessage()."
    ";
  155. }
  156. }
  157. ?>

人气教程排行