55

I have PHP code that is used to add variables to a session:

<?php
    session_start();
    if(isset($_GET['name']))
    {
        $name = isset($_SESSION['name']) ? $_SESSION['name'] : array();
        $name[] = $_GET['name'];
        $_SESSION['name'] = $name;
    }
    if (isset($_POST['remove']))
    {
        unset($_SESSION['name']);
    }
?>
<pre>  <?php print_r($_SESSION); ?>  </pre>

<form name="input" action="index.php?name=<?php echo $list ?>" method="post">
  <input type="submit" name ="add"value="Add" />
</form>

<form name="input" action="index.php?name=<?php echo $list2 ?>" method="post">
  <input type="submit" name="remove" value="Remove" />
</form>

I want to remove the variable that is shown in $list2 from the session array when the user chooses 'Remove'.

But when I unset, ALL the variables in the array are deleted.

How I can delete just one variable?

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
LiveEn
  • 3,193
  • 12
  • 59
  • 104

7 Answers7

65
if (isset($_POST['remove'])) {
    $key=array_search($_GET['name'],$_SESSION['name']);
    if($key!==false)
    unset($_SESSION['name'][$key]);
    $_SESSION["name"] = array_values($_SESSION["name"]);
} 

Since $_SESSION['name'] is an array, you need to find the array key that points at the name value you're interested in. The last line rearranges the index of the array for the next use.

Hubyx Reds
  • 113
  • 1
  • 6
dnagirl
  • 20,196
  • 13
  • 80
  • 123
48

To remove a specific variable from the session use:

session_unregister('variableName');

(see documentation) or

unset($_SESSION['variableName']);

NOTE: session_unregister() has been DEPRECATED as of PHP 5.3.0 and REMOVED as of PHP 5.4.0.

shadyyx
  • 15,825
  • 6
  • 60
  • 95
Andreas
  • 5,305
  • 4
  • 41
  • 60
  • it drops all the values from the array, not singles :( – LiveEn Feb 09 '10 at 19:26
  • If the variable you are trying to "drop"/"unset" is an array then the array will be removed. e.g. $_SESSION['myarray'] = array('key'=>val,'key2'=>val2); usnet($_SESSION['myarray'] will unset the array...but unset($_SESSION['myarray']['key2'] will remove the second array element - (key,value) pair – Andreas Feb 09 '10 at 22:32
  • 3
    session_unregister() "This function has been DEPRECATED as of PHP 5.3.0 and REMOVED as of PHP 5.4.0." – Yasen Oct 11 '12 at 11:55
  • use unset ($_SESSION["name"]); – Arpit Patel Oct 19 '18 at 22:51
6

Is the $_SESSION['name'] variable an array? If you want to delete a specific key from within an array, you have to refer to that exact key in the unset() call, otherwise you delete the entire array, e.g.

$name = array(0 => 'a', 1 => 'b', 2 => 'c');
unset($name); // deletes the entire array
unset($name[1]); // deletes only the 'b' entry

Another minor problem with your snippet: You're mixing GET query parameters in with a POST form. Is there any reason why you can't do the forms with 'name' being passed in a hidden field? It's best to not mix get and post variables, especially if you use $_REQUEST elsewhere. You can run into all kinds of fun trying to figure out why $_GET['name'] isn't showing up the same as $_POST['name'], because the server's got a differnt EGPCS order set in the 'variables_order' .ini setting.

<form blah blah blah method="post">
  <input type="hidden" name="name" value="<?= htmlspecialchars($list1) ?>" />
  <input type="submit" name="add" value="Add />
</form>

And note the htmlspecialchars() call. If either $list1 or $list2 contain a double quote ("), it'll break your HTML

Marc B
  • 356,200
  • 43
  • 426
  • 500
4

If you want to remove or unset all $_SESSION 's then try this

session_destroy();

If you want to remove specific $_SESSION['name'] then try this

session_unset('name');
Asad Ali
  • 655
  • 7
  • 10
1

Currently you are clearing the name array, you need to call the array then the index you want to unset within the array:

$ar[0]==2
$ar[1]==7
$ar[2]==9

unset ($ar[2])

Two ways of unsetting values within an array:

<?php
# remove by key:
function array_remove_key ()
{
  $args  = func_get_args();
  return array_diff_key($args[0],array_flip(array_slice($args,1)));
}
# remove by value:
function array_remove_value ()
{
  $args = func_get_args();
  return array_diff($args[0],array_slice($args,1));
}

$fruit_inventory = array(
  'apples' => 52,
  'bananas' => 78,
  'peaches' => 'out of season',
  'pears' => 'out of season',
  'oranges' => 'no longer sold',
  'carrots' => 15,
  'beets' => 15,
);

echo "<pre>Original Array:\n",
     print_r($fruit_inventory,TRUE),
     '</pre>';

# For example, beets and carrots are not fruits...
$fruit_inventory = array_remove_key($fruit_inventory,
                                    "beets",
                                    "carrots");
echo "<pre>Array after key removal:\n",
     print_r($fruit_inventory,TRUE),
     '</pre>';

# Let's also remove 'out of season' and 'no longer sold' fruit...
$fruit_inventory = array_remove_value($fruit_inventory,
                                      "out of season",
                                      "no longer sold");
echo "<pre>Array after value removal:\n",
     print_r($fruit_inventory,TRUE),
     '</pre>';
?> 

So, unset has no effect to internal array counter!!!

http://us.php.net/unset

James Campbell
  • 5,057
  • 2
  • 35
  • 54
0

Try this one:

if(FALSE !== ($key = array_search($_GET['name'],$_SESSION['name'])))
{
    unset($_SESSION['name'][$key]);
}
dev-null-dweller
  • 29,274
  • 3
  • 65
  • 85
0

Simply use this method.

<?php
    $_SESSION['foo'] = 'bar'; // set session 
    print $_SESSION['foo']; //print it
    unset($_SESSION['foo']); //unset session 
?>
Arunraj S
  • 758
  • 7
  • 26