欢迎来到代码驿站!

PHP代码

当前位置:首页 > 软件编程 > PHP代码

php同时使用session和cookie来保存用户登录信息的实现代码

时间:2021-01-14 11:13:25|栏目:PHP代码|点击:

1、用户登录状态操作类UserLogin

<?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);
 }

}

?>

2、在用户输入用户名、密码处调用来做相关判定

<?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>";
 }
}

?>

上一篇:Fatal error: Call to undefined function curl_init()解决方法

栏    目:PHP代码

下一篇:Yii框架核心组件类实例详解

本文标题:php同时使用session和cookie来保存用户登录信息的实现代码

本文地址:http://www.codeinn.net/misctech/44936.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有