views:

545

answers:

5

below is a 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 need to remove the variable that is shown in $list2 from the session array when the user wants to remove . But when i unset, all the variables in the array are deleted. Can some one please tell me how can i delete one variable?

Thanks

A: 

To remove a specific variable from the session use:

session_unregister() (see documentation)

or 

unset($_SESSION['variableName']);
andreas
it drops all the values from the array, not singles :(
LiveEn
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...butunset($_SESSION['myarray']['key2'] will remove the second array element - (key,value) pair
andreas
A: 

Try this one:

if(FALSE !== ($key = array_search($_GET['name'],$_SESSION['name'])))
{
    unset($_SESSION['name'][$key]);
}
dev-null-dweller
A: 

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
+1  A: 
if (isset($_POST['remove'])) {
    $key=array_search($_GET['name'],$_SESSION['name']);
    if($key!==false) unset($_SESSION['name'][$key]);
} 

Since $_SESSION['name'] is an array, you need to find the array key that points at the name value you're interested in.

dnagirl
A: 

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