Session Unset - Complex solution required

Not sure if this is possible, but always worth a check before I give up on it.
Can sessions be “unset” based on matching a particular pattern?

EG: Say there are five session variables.
$_SESSION[‘bar_id’];
$_SESSION[‘foo_id’];
$_SESSION[‘foo_name’];
$_SESSION[‘foo_phone’];
$_SESSION[‘foo_email’];

Now let’s say I want to destroy all the “foo” sessions? Is there an easy way to unset (or null) those without taking out the “bar” session.

I know session_destroy() will take them all out, and I know I can manage each one independently with unset, or by re-assigning the value to Null, but I would love a solution to unset everything matching a condition.

Possible? Or am I out of luck?

Use arrays, Luke!

Can you give an example of what you mean by “unset everything matching a condition”? What sort on conditions would they be?

@N5_Shrapnel, codamedia already is using an array, the $_SESSION array:D

@Shrapnel: Session are arrays - so yes, I guess I am using them :slight_smile: That still doesn’t make me understand how to solve my problem.

@SpacePhoenix: In this example - I simply want to unset everything that has “foo” in the session name, but leave the rest (in this case with “bar”). I don’t want to (or need to) match the values of those variables, just the names.


foreach($_SESSION as$key => $value){
    if('foo' === substr($key, 0, 3)){
        unset($_SESSION[$key]);
    }
}


$_SESSION['foo'] = array();
unset($_SESSION['foo']);

Are the sessions being stored as temp files on the server or in a database table?

unset ($_SESSION[‘foo’]);
VERY easy
when you’re using array

Got it! I wasn’t sure what you were getting at first time, but now it’s clear. Thanks.

Thanks for the pointers. Your sample(s) along with Shrapnels suggestion of arrays made things pretty clear.

Yes.