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条)

报告相同问题?