【问题标题】:How to destroy current session but not other session如何销毁当前会话而不是其他会话
【发布时间】:2020-05-03 08:13:49
【问题描述】:

我有一个简单的登录系统,其中有两种类型的用户(角色:0,1)。如果用户是角色 0,则用户被重定向到 search.php,否则角色为 1,重定向到 overview.php。

if ($role == 0){
  session_regenerate_id();
  $_SESSION['loggedin'] = TRUE;
  $_SESSION['name'] = $_POST['email'];
  $_SESSION['id'] = $id;
  header('Location: search.php');
} elseif ($role == 1) {
  session_regenerate_id();
  $_SESSION['loggedin'] = TRUE;
  $_SESSION['user'] = $name;
  $_SESSION['name'] = $_POST['email'];
  $_SESSION['id'] = $id;
  header('Location: overview.php');
}

我可以注销和销毁会话,但是如果两个用户都登录并且一个用户注销,它将结束两个用户的会话。 这是我的 logout.php:

<?php
// Initialize the session
session_start();

// Destroy the session.
session_destroy();

header('Location: login.php');
exit;
?>

然后我找到了这个解决方案source。我不确定如何获取 to_destroy_id ($des),所以我将其设置为当前会话 ID。 这是我更新的logout.php:

<?php
$des = session_id();
// 1. commit session if it's started.
if (session_id()) {
    session_commit();
}

// 2. store current session id
session_start();
$current_session_id = session_id();
session_commit();

// 3. hijack then destroy session specified.
session_id($des);
session_start();
session_destroy();
session_commit();

// 4. restore current session id. If don't restore it, your current session will refer to the session you just destroyed!
session_id($current_session_id);
session_start();
session_commit();
// Redirect to the login page:
header('Location: restTablet.php');

?>

这是第一次工作,然后又停止工作。如果一个用户注销,则每个人都注销。 如果他们单击注销,我只想销毁用户会话,并且其他用户保持登录状态。知道如何实现吗?

更新:对 logout.php 进行以下更改,如果一个用户注销,我可以让其他人保持登录状态,但是一旦用户注销并尝试返回,用户就可以在不登录的情况下再次访问它。这是logout.php:

<?php
$des = session_id();
// 1. commit session if it's started.
if (session_id()) {
    session_commit();
}

// 2. store current session id
session_start();
$current_session_id = session_id();
session_commit();

// 3. hijack then destroy session specified.
session_id($des);
session_start();
session_destroy();
session_commit();

// Redirect to the login page:
header('Location: gabLogin.php');
?>

【问题讨论】:

  • $role值存储在哪里?
  • 退出 1 个用户当然不会影响其他用户。您确定吗?你如何看待这个问题,我的意思是描述你如何看待它。您的会话存储和配置是什么?
  • 角色存储在sql数据库中。

标签: php mysql session


【解决方案1】:

您可以将 $_SESSION 数据嵌套在父级中。 例如,您有两个角色,role 1role 2。 像下面这样设置$_SESSION

if ($role == 0){
  session_regenerate_id();
  $_SESSION['role_0']['loggedin'] = TRUE;
  $_SESSION['role_0']['name'] = $_POST['email'];
  $_SESSION['role_0']['id'] = $id;
  header('Location: search.php');
} elseif ($role == 1) {
  session_regenerate_id();
  $_SESSION['role_1']['loggedin'] = TRUE;
  $_SESSION['role_1']['user'] = $name;
  $_SESSION['role_1']['name'] = $_POST['email'];
  $_SESSION['role_1']['id'] = $id;
  header('Location: overview.php');
}

然后当您的用户退出 role_0 时,仅取消设置该角色的父会话值。

//use logic in logout form to POST proper logout for that role.
if(isset($_POST['logout_0'])){ //--> role_0 is logging out
  unset($_SESSION['role_0']); //--> all child data for role_0 should be unset now.
  //--> check if user is logged in as alternate role
  if($_SESSION['role_1']['loggedin'] === TRUE){
    header('Location: overview.php');
  }else{
    //--> redirect to the page you wish them to go to when logged out
  }
}

【讨论】:

  • 这不是处理登录会话的完美方式
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-12-08
  • 2015-09-09
  • 2018-02-13
  • 2012-09-23
  • 2011-11-25
  • 1970-01-01
相关资源
最近更新 更多