Php user logon status operation class written based on session and cookie

Source: Internet
Author: User
Php user logon status operation class written based on session and cookie

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

2. the user enters the user name and password for relevant determination.

  1. Require_once 'init. php ';

  2. // If logged in, logout

  3. If (UserLogin: isLogin () & $ _ COOKIE ["user_id"] = 1 ){
  4. UserLogin: delUserInfo ();
  5. }
  6. Else if (UserLogin: isLogin ()){
  7. Utils: redirect ('Welcome ');
  8. }

  9. $ Username = null;

  10. $ Password = null;

  11. $ Msg = "";

  12. If (isset ($ _ POST ['username']) & isset ($ _ POST ['password']) {

  13. $ Username = addslashes (trim (stripslashes ($ _ POST ['username']);
  14. $ Password = addslashes (trim (stripslashes ($ _ POST ['password']);
  15. // Validate
  16. $ Errors = LoginValidator: validate ($ username, $ password );
  17. If (empty ($ errors )){
  18. // Save
  19. $ Dao = new UserDao ();
  20. $ User = $ dao-> findByName ($ username );
  21. $ Last_login_ip = Utils: getIpAddress ();
  22. $ User-> setLastLoginIp ($ last_login_ip );
  23. $ Now = new DateTime ();
  24. $ User-> setLastLoginTime ($ now );
  25. $ Dao-> save ($ user );
  26. UserLogin: setUserInfo ($ user );
  27. Flash: addFlash ('login successful! ');
  28. Utils: redirect ('Welcome ');
  29. }
  30. Foreach ($ errors as $ e ){
  31. $ Msg. = $ e-> getMessage ()."
    ";
  32. }
  33. }
  34. ?>

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.