A login verification class written in php

Source: Internet
Author: User
A login verification class written in php

  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 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', 'user name cannot be blank. ');
  71. } Elseif (strlen ($ username) <3 ){
  72. $ Errors [] = new Error ('username', 'user name length cannot be less than 3 characters. ');
  73. } Elseif (strlen ($ username)> 30 ){
  74. $ Errors [] = new Error ('username', 'the username length cannot exceed 30 characters. ');
  75. } Elseif (! Preg_match ('/^ [A-Za-z] + $/', substr ($ username, 0, 1 ))){
  76. $ Errors [] = new Error ('username', 'the user name must start with a letter. ');
  77. } Elseif (! Preg_match ('/^ [A-Za-z0-9 _] + $/', $ username )){
  78. $ Errors [] = new Error ('username', 'user name can only be a combination of letters, numbers, and underscores. ');
  79. } Elseif (! Trim ($ password )){
  80. $ Errors [] = new Error ('password', 'the password cannot be blank. ');
  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', 'the user name or password is incorrect. ');
  88. }
  89. } Else {
  90. $ Errors [] = new Error ('username', 'user name does not exist. ');
  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 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, the jump class of the logout page will not be repeated here in the 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 ('login successful! ');
  151. Utils: redirect ('Welcome ');
  152. }
  153. Foreach ($ errors as $ e ){
  154. $ Msg. = $ e-> getMessage ()."
    ";
  155. }
  156. }
  157. ?>

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.