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 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常
  • ¥15 关于风控系统,如何去选择
  • ¥15 这款软件是什么?需要能满足我的需求
  • ¥15 SpringSecurityOauth2登陆前后request不一致