PHP user login status operation class written based on session and cookie

WBOY
Release: 2016-07-25 09:03:09
Original
920 people have browsed it
  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、在用户输入用户名、密码处调用来做相关判定

  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('登录成功!');
  28. Utils::redirect('welcome');
  29. }
  30. foreach ($errors as $e) {
  31. $msg .= $e->getMessage()."
    ";
  32. }
  33. }
  34. ?>

复制代码


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template