部分删除/销毁 $_SESSION 数据? PHP
我正在寻找一种方法来仅删除存储的一定数量的会话数据,同时保留与登录用户关联的会话数据。
目前,我正在通过对要删除的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
无法删除“SESSION 数组的整个部分,同时保留特定变量”。相反,您可以使用二维数组来执行任务并删除该数组。
当任务1完成删除时,像
现在一样$_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.
when task1 complete delete like
now $_SESSION["task2"] still exist.
好吧,您可以将所有这些易失性数据存储在另一个键中:
如果您不想这样做,您可以使用数组比较函数,例如:
就数据库而言,您可以这样做,但它没有必要实现您的目标,除非有移动您在原始问题 ID 中未列出的部分表示您现在可能不需要做任何复杂的事情。
Well you could store all this volatile data inside another key:
If you dnt want to do that you could use array comparison functions like:
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.
按层次结构构建会话数据:
Structure your session data in a hierarchy:
我不得不不同意@sathishkumar,以下方法会部分破坏会话变量。
在 session_destroy 函数的 php 文档中,我们可以读到:
因此,“技巧”是在 session_destroy 之后调用 session_start。
希望这有帮助。
I will have to disagree with @sathishkumar, the following method destroys partially session variables.
In the php docs for the session_destroy function, we can read this:
So, the "trick" is to call session_start AFTER session_destroy.
Hope this helps.