Maison > développement back-end > tutoriel php > php写的一个登录验证类

php写的一个登录验证类

WBOY
Libérer: 2016-07-25 09:04:24
original
866 Les gens l'ont consulté
  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) $errors[] = new Error('username', '用户名长度不能小于3个字符。');
  72. } elseif (strlen($username)>30) {
  73. $errors[] = new Error('username', '用户名长度不能超过30个字符。');
  74. } elseif (!preg_match('/^[A-Za-z]+$/',substr($username, 0, 1))) {
  75. $errors[] = new Error('username', '用户名必须以字母开头。');
  76. } elseif (!preg_match('/^[A-Za-z0-9_]+$/', $username)) {
  77. $errors[] = new Error('username', '用户名只能是字母、数字以及下划线( _ )的组合。');
  78. } elseif (!trim($password)) {
  79. $errors[] = new Error('password', '密码不能为空。');
  80. } else {
  81. // check whether use exists or not
  82. $dao = new UserDao();
  83. $user = $dao->findByName($username);
  84. if ($user) {

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

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

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

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

  118. * Get error message.
  119. * @return string error message
  120. */
  121. public function getMessage() {
  122. return $this->message;
  123. }
  124. }
  125. // if logged in, logout 页面的跳转类在http://www.cnblogs.com/setsail/archive/2012/12/18/2823231.html 里这里不再重复书写

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

  132. $password = null;
  133. $msg = "";
  134. if (isset($_POST['username']) && isset($_POST['password'])) {

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


Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal