dongxun7962 2012-01-27 09:23
浏览 32
已采纳

php - 这是否可以从其他会话中取消设置一个变量?

If I want to unset one variable in current user session then I can use command unset($_SESSION['something']).

Is this possible to unset only one variable from other session in case when I know PHPSESSID value (can I do it without changing current user session)? I would like check if session of PHPSESSID still exist, if exist then unset($_SESSION['something']) of chosen PHPSESSID.

  • 写回答

3条回答 默认 最新

  • dqifn68206 2012-01-27 12:29
    关注

    Is this possible to unset only one variable from other session in case when I know PHPSESSID value

    Yes this is possible and you need to know the session id of that session you would like to unset the variable/value from.

    can I do it without changing current user session?

    If you want to do it with standard PHP functions, it's not possible without switching the session. With third-party libraries you can do that w/o switching the session.

    I would like check if session of PHPSESSID still exist, if exist then unset($_SESSION['something']) of chosen PHPSESSID.

    This is pretty similar to an answer to php destroy a session which is not the current session, but only deleting a specific member:

    $unsetFromSessionID = ... ; # set your session id from where you want to unset from
    $unsetVariableName = 'something'; # set the variable name
    
    $backupSessionID = session_id($unsetFromSessionID);
    session_start(); # load session data
    unset($_SESSION[$unsetVariableName]);
    session_commit(); # save changes to disc
    
    session_id($backupSessionID); # switch to current session
    session_start();
    

    If you want to find out if that other session was active or not, this is not possible with this method and from within PHP because PHP will create a new, empty session on session_start() in case it did not exists.

    The alternative is to work with the session store directly, e.g. by looking for the session file on disc, loading it's contents, removing a variable and saving it back. A PHP library that is able to do that is Serialized, it ships with an example of a Session File Viewer which might be a good starting point.

    See also: How to tell if a session is active?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 hexo+github部署博客
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?