How to unset/destroy all session data except some specific keys?

后端 未结 5 1199
清酒与你
清酒与你 2020-12-19 10:59

I have some session data in a website. I want to destroy all session data when user click another page, except some specific keys like $_SESSION[\'x\'] and

5条回答
  •  太阳男子
    2020-12-19 11:43

    As $_SESSION is a regular array, you can use array_intersect_key to get your resulting array:

    $keys = array('x', 'y');
    $_SESSION = array_intersect_key($_SESSION, array_flip($keys));
    

    Here array_flip is used to flip the key/value association of $keys and array_intersect_key is used to get the intersection of both arrays while using the keys for comparison.

提交回复
热议问题