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

后端 未结 5 1185
清酒与你
清酒与你 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条回答
  •  萌比男神i
    2020-12-19 11:51

    to unset a particular session variable use.

    unset($_SESSION['one']);
    

    to destroy all session variables at one use.

    session_destroy()

    To free all session variables use.

    session_unset();

    if you want to destroy all Session variable except x and y you can do something like this.

    $requiredSessionVar = array('x','y');
    foreach($_SESSION as $key => $value) {
        if(!in_array($key, $requiredSessionVar)) {
            unset($_SESSION[$key]);
        }
    }
    

提交回复
热议问题