Home > Backend Development > PHP Tutorial > PHP uses session and cookie at the same time to save the implementation code of user login information, sessioncookie_PHP tutorial

PHP uses session and cookie at the same time to save the implementation code of user login information, sessioncookie_PHP tutorial

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-07-12 08:51:52
Original
890 people have browsed it

php uses both session and cookie to save the implementation code of user login information, sessioncookie

1. User login status operation class UserLogin

<&#63;php

final class UserLogin {

 public function __construct() {
  
 }

 public static function getUserInfo() {
  if (isset($_COOKIE["user_id"])&&$_COOKIE["user_id"]&&(trim($_COOKIE["user_id"])!="")) {
   if (isset($_SESSION["USER_INFO"]))
    return $_SESSION["USER_INFO"];
   $dao = new UserDao();
   $user = $dao->find($_COOKIE["user_id"]);
   if ($user) {
    $_SESSION["USER_INFO"] = $user;
    setcookie("docloud_sid", session_id(), time() + 36000);
    setcookie("user_id", $_COOKIE["user_id"], time() + 36000);
    
    if (array_key_exists("selected_prj_id", $_COOKIE))
     setcookie("selected_prj_id", $_COOKIE["selected_prj_id"], time() + 36000);
    
    if (array_key_exists("selected_class_id", $_COOKIE))
     setcookie("selected_class_id", $_COOKIE["selected_class_id"], time() + 36000);
    
    if (array_key_exists("selected_image_id", $_COOKIE))
     setcookie("selected_image_id", $_COOKIE["selected_image_id"], time() + 36000);
    
    if (array_key_exists("test_image_ids", $_COOKIE))
     setcookie("test_image_ids", $_COOKIE["test_image_ids"], time() + 36000);
    
    if (array_key_exists("upload_image_ids", $_COOKIE))
     setcookie("upload_image_ids", $_COOKIE["upload_image_ids"], time() + 36000);
    return $user;
   }
  }
  self::clearCookie();
  return null;
 }

 public static function setUserInfo($userInfo) {
  $_SESSION["USER_INFO"] = $userInfo;
  setcookie("docloud_sid", session_id(), time() + 36000);
  setcookie("user_id", $userInfo->getId(), time() + 36000);
 }

 public static function isLogin() {
  if (self::getUserInfo()) {
   return true;
  }
  return false;
 }

 public static function delUserInfo() {
  self::clearCookie();
  session_destroy();
 }
 
 private static function clearCookie() {
  setcookie("docloud_sid", "", time() - 36000);
  setcookie("user_id", "", time() - 36000);
  setcookie("selected_prj_id", "", time() - 36000);
  setcookie("selected_class_id", "", time() - 36000);
  setcookie("selected_image_id", "", time() - 36000);
  setcookie("test_image_ids", "", time() - 36000);
  setcookie("upload_image_ids", "", time() - 36000);
 }

}

&#63;>
Copy after login

2. Call when the user enters the user name and password to make relevant judgments

<&#63;php
require_once 'Init.php';

// if logged in, logout
if (UserLogin::isLogin() && $_COOKIE["user_id"]==1) {
 UserLogin::delUserInfo();
}
else if (UserLogin::isLogin()){
 Utils::redirect('welcome');
}

$username = null;
$password = null;

$msg = "";

if (isset($_POST['username']) && isset($_POST['password'])) {
 $username = addslashes(trim(stripslashes($_POST ['username'])));
 $password = addslashes(trim(stripslashes($_POST ['password'])));
 // validate
 $errors = LoginValidator::validate($username, $password);
 
 if (empty($errors)) {
  // save
  $dao = new UserDao();
  $user = $dao->findByName($username);
  $last_login_ip = Utils::getIpAddress();
  $user->setLastLoginIp($last_login_ip);
  $now = new DateTime();
  $user->setLastLoginTime($now);
  $dao->save($user);
  UserLogin::setUserInfo($user);
  Flash::addFlash('登录成功!');
  Utils::redirect('welcome');
 }
 
 foreach ($errors as $e) {
  $msg .= $e->getMessage()."<br>";
 }
}

&#63;>
Copy after login

The implementation code of the above PHP using session and cookie to save user login information is all the content shared by the editor. I hope it can give you a reference, and I also hope that everyone will support Bangkejia.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1127924.htmlTechArticlephp uses both session and cookie to save the implementation code of user login information, sessioncookie 1. User login status operation class UserLogin phpfinal class UserLogin { public function __cons...
Related labels:
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
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template