4 Replies - 30528 Views - Last Post: 25 June 2011 - 05:02 AM Rate Topic: -----

#1 Bbob   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 09-September 10

How to unset multiple session variables with specific name.

Posted 24 June 2011 - 07:37 AM

Hi

How do I unset session variables with specific names?

I would like to unset all session variables with this name that start with "rock_"
and ends with a $id (let's assume the $id = 1, I want to unset all session variable that ends with 1)

like this.

$_SESSOIN['rock_'.$type.'_'.$id]
Is This A Good Question/Topic? 0
  • +

Replies To: How to unset multiple session variables with specific name.

#2 codeprada   User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 963
  • View blog
  • Posts: 2,382
  • Joined: 15-February 11

Re: How to unset multiple session variables with specific name.

Posted 24 June 2011 - 07:47 AM

You'd have to go through the array with a
foreach($array as $key => $value)
.

Example
$array = array("a" => 0, "b" => 1, "c" => 2);
foreach($array as $key => $value)
{
	if($key == "b") //do your testing here
		unset($array[$key]);
}
print_r($array);

In your loop you'd have to run a preg_match function on the array key to see which keys to delete.
Was This Post Helpful? 0
  • +
  • -

#3 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3837
  • View blog
  • Posts: 13,997
  • Joined: 08-August 08

Re: How to unset multiple session variables with specific name.

Posted 24 June 2011 - 08:41 AM

Didn't we just cover this?
Ok, it's not exactly the same thing, but if you put your values in a class you could define a class method to reset values. That's part of why it's so much easier than trying to parse a string.
Was This Post Helpful? 0
  • +
  • -

#4 Bbob   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 09-September 10

Re: How to unset multiple session variables with specific name.

Posted 25 June 2011 - 01:10 AM

@codeprada

Hi

I tried the code below and it doesn't work. The session is not unsetting since it still echoes after the unset(); as well as when I press back on my browser.
  foreach($_SESSION as $rock => $value){
				if($value > 0) {
					if(substr($rock, 0, 5)=='rock_'){
						
						$info =  (explode("_", $rock));
						
						$c = $info[0]; 
						$i = $info[1]; 
						$o = $info[2]; 
					
						if($o == $oid){
							unset($_SESSION[$rock]);
							echo $_SESSION[$rock];
						}
						else{
							echo "Rock cannot be unset";
						}







@CTphpnwb

Hi

I'm not familiar with classes. I thought they were only in Java :D (I'm not really good this :( )

From your code...am I correct that it is required to check all possible values in the session? I'm asking because the $name &id would come from the database and the database would constantly be updated so there would be alot of checking and I'd have to create another loop to create new session check - this is what I understand sorry if Im wrong.

This post has been edited by Bbob: 25 June 2011 - 01:16 AM

Was This Post Helpful? 0
  • +
  • -

#5 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3837
  • View blog
  • Posts: 13,997
  • Joined: 08-August 08

Re: How to unset multiple session variables with specific name.

Posted 25 June 2011 - 05:02 AM

The class can store $name and $id, just as an array can, and just as your concatenated string can. Your code:
$_SESSION['rock_'.$type.'_'.$id'];

does the same thing as putting an object in a session variable. If one needs to be updated from the database, then so does the other. If one doesn't then neither does. The difference is that it's much easier to access the class variables than it is to parse a string.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1