部分删除/销毁 $_SESSION 数据? PHP

发布于 2024-12-28 23:54:56 字数 445 浏览 1 评论 0原文

我正在寻找一种方法来仅删除存储的一定数量的会话数据,同时保留与登录用户关联的会话数据。

目前,我正在通过对要删除的 SESSION 变量进行单独的 unset 语句来执行此操作。

然而,我希望可能有一种更聪明的方法来删除 SESSION 数组的整个部分,同时保留特定变量,

例如

$_SESSION['username'];
$_SESSION['user_id'];
$_SESSION['ttl'];

此过程的用例是:

用户登录 -->用户执行任务 -->任务完成后,删除与任务关联的会话数据 -->用户仍处于登录状态!

我曾考虑过在数据库中使用一个表来监控登录,您对此有何看法?

感谢您抽出时间!

I am looking for a way to delete only certain amounts of SESSION data that is stored whilst preserving the session data associated with the user being logged on.

At the moment I am doing this by individual unset statements to the SESSION variables I want to delete.

However I was hoping there may be a more clever way to just delete a whole section of the SESSION array whilst preserving specific variables

e.g.

$_SESSION['username'];
$_SESSION['user_id'];
$_SESSION['ttl'];

The use case for this process would be:

User Logs In --> User performs task --> Once task is complete delete session data associated with task --> User is still logged in!

I had considered perhaps using a table in my Database monitoring logins, what are your opinions on that?

Thanks for your time!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

眼眸印温柔 2025-01-04 23:54:57

无法删除“SESSION 数组的整个部分,同时保留特定变量”。相反,您可以使用二维数组来执行任务并删除该数组。

$_SESSION["task1"]["username"] = "name"
$_SESSION["task1"]["pass"] = "pass"

$_SESSION["task2"]["name"] = "name";

当任务1完成删除时,像

  unset($_SESSION["task1"]);

现在一样$_SESSION[“task2”]仍然存在。

There is no way to delete "whole section of the SESSION array whilst preserving specific variables".Instead of that you can use two dimensional array for a task and delete that array.

$_SESSION["task1"]["username"] = "name"
$_SESSION["task1"]["pass"] = "pass"

$_SESSION["task2"]["name"] = "name";

when task1 complete delete like

  unset($_SESSION["task1"]);

now $_SESSION["task2"] still exist.

染年凉城似染瑾 2025-01-04 23:54:57

好吧,您可以将所有这些易失性数据存储在另一个键中:

$_SESSION['volatile'] = array(
   'one' => 'value'
);

如果您不想这样做,您可以使用数组比较函数,例如:

// specify what keys to keep
$_SESSION = array_intersect_key($_SESSION, array('keepme1', 'keepme2', 'etc'));

//specify what keys to remove
$_SESSION = array_diff_key($_SESSION, array('deleteme1', 'deleteme2', 'etc'));

就数据库而言,您可以这样做,但它没有必要实现您的目标,除非有移动您在原始问题 ID 中未列出的部分表示您现在可能不需要做任何复杂的事情。

Well you could store all this volatile data inside another key:

$_SESSION['volatile'] = array(
   'one' => 'value'
);

If you dnt want to do that you could use array comparison functions like:

// specify what keys to keep
$_SESSION = array_intersect_key($_SESSION, array('keepme1', 'keepme2', 'etc'));

//specify what keys to remove
$_SESSION = array_diff_key($_SESSION, array('deleteme1', 'deleteme2', 'etc'));

As far as the DB you could do that but its not necessary to accomplish your objective, and unless there are moving parts you didnt list in your original question id say you probably dont need to do anything that complex right now.

爱殇璃 2025-01-04 23:54:57

按层次结构构建会话数据:

$_SESSION['loggedIn'] = TRUE;

// Temporary session data
$_SESSION['temporary'] = array(
    'temp_var1' => 'foo',
    'temp_var2' => 'bar',
    // ...
    'temp_var99' => 'baz'
);

echo $_SESSION['temporary']['temp_var2']; // bar

// Remove all temporary session data
unset($_SESSION['temporary']);

echo $_SESSION['loggedIn'] ? 'yes' : 'no'; // yes

Structure your session data in a hierarchy:

$_SESSION['loggedIn'] = TRUE;

// Temporary session data
$_SESSION['temporary'] = array(
    'temp_var1' => 'foo',
    'temp_var2' => 'bar',
    // ...
    'temp_var99' => 'baz'
);

echo $_SESSION['temporary']['temp_var2']; // bar

// Remove all temporary session data
unset($_SESSION['temporary']);

echo $_SESSION['loggedIn'] ? 'yes' : 'no'; // yes
你是暖光i 2025-01-04 23:54:57

我不得不不同意@sathishkumar,以下方法会部分破坏会话变量。

public static function destroyPartial($keys)
{

    if (session_status() === \PHP_SESSION_NONE) {
        session_start();
    }

    if (!is_array($keys)) {
        $keys = [$keys];
    }
    foreach ($_SESSION as $k => $v) {
        if (in_array($k, $keys, true)) {
            unset($_SESSION[$k]);
        }
    }



    $recoveringSession = $_SESSION;
    session_destroy();
    session_start();
    $_SESSION = $recoveringSession;
}

在 session_destroy 函数的 php 文档中,我们可以读到:

session_destroy() 销毁与当前会话关联的所有数据
会议。它不会取消设置与关联的任何全局变量
会话,或取消设置会话 cookie。使用会话变量
再次,必须调用 session_start()。

因此,“技巧”是在 session_destroy 之后调用 session_start。

希望这有帮助。

I will have to disagree with @sathishkumar, the following method destroys partially session variables.

public static function destroyPartial($keys)
{

    if (session_status() === \PHP_SESSION_NONE) {
        session_start();
    }

    if (!is_array($keys)) {
        $keys = [$keys];
    }
    foreach ($_SESSION as $k => $v) {
        if (in_array($k, $keys, true)) {
            unset($_SESSION[$k]);
        }
    }



    $recoveringSession = $_SESSION;
    session_destroy();
    session_start();
    $_SESSION = $recoveringSession;
}

In the php docs for the session_destroy function, we can read this:

session_destroy() destroys all of the data associated with the current
session. It does not unset any of the global variables associated with
the session, or unset the session cookie. To use the session variables
again, session_start() has to be called.

So, the "trick" is to call session_start AFTER session_destroy.

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文