hi
i have a simple script that adds an item id to a session...

<?php
session_start();

$itemid = $_POST["itemid"];

if (count($SESSION["cart"]) < 1) {
$
SESSION["cartitems"] = array();
}

$_SESSION["cart"][] = $itemid;

header("location:cart.php");
?>

i need something that will delete an id out of the array so the cart wont display this.

this is what i tried but nothing was change but no errors occured,

<?php
session_start();

$id=$_POST["id"];

unset($_SESSION['cart']['$id']);

header("location:cart.php");

?>

i would be gratefull of any help,
many thanks

    That's because variable interpolation doesn't happen when you use single quotes. Example:

    $foo = 'bar';
    
    echo 'The variable is: $foo'; // outputs: The variable is: $foo
    echo "The variable is: $foo"; // outputs: The variable is: bar

    More information about this can be found on the manual page: [man]string[/man].

    In fact, when using variables as array indeces (or function parameters), you don't even need to use a string at all (ex. $_SESSION['cart'][$id] would work).

    Also, when posting PHP code, please use the board's [PHP][/PHP] bbcode tags as they make your code much easier to read and analyze.

      thanks but i still cant get it to work the cart works fine i just cant delete the items, i have a form on the cart...

      <form id="form1" name="form1" method="post" action="/remove_item.php">
      <input name="Submit" type="submit" class="formbox" value="x" />
      <input name="id" type="hidden" id="id" value="<? echo $info['id']; ?>" />
      </form> 
      

      then the remove is now...

      <?php
      session_start();

      unset($_SESSION['cart'][$id]);

      header("location:cart.php");

      ?>

      but the cart stays the same

        In your final example, you don't set the value of $id.

        Also, I have read of people having problems with session data not being saved when they do a header() redirect, so you might want to save the session before that:

        <?php
        if(isset($_POST['id']))
        {
           // at this point, you should filter the input -- I'll assume it's an integer for now
           $id = (int) $_POST['id'];
           unset($_SESSION['cart'][$id]);
           session_write_close(); // <------- write the session data before redirecting
           header("Location: http://www.example.com/cart.php");
        }
        else
        {
           // handle lack of $_POST['id'] error here
        }
        

          nope... just goes back to the original cart.
          maybe its my browser - is there any thing wrong with the add to cart bit -

          <?php
          session_start();
          
          $itemid = $_POST["itemid"];
          
          if (count($_SESSION["cart"]) < 1) {
          $_SESSION["cartitems"] = array();
          }
          
          
          $_SESSION["cart"][] = $itemid;
          
          
          header("location:cart.php");
          ?>
          

          ????
          http://tom.btronics.co.uk/cart.php

            the form is sending the id because if an echo is applied in place of the header the items database id is displayed so how can this be deleted, also some how when i exit my browser the session is not deleted???

            thanks in advance
            ben

              hi!
              I am a bit confused.

              You use 'cartitems' and 'cart'.
              What is the relation between those 2 variables?
              And where does 'cartitems' get any value?
              And why you need 2 different arrays in session?

              I mean when I go to the shop, I need only one cart to put my things.

              My guess is you have not yet shown us everything ...

              if (count($_SESSION["cart"]) < 1) {
              $_SESSION["cartitems"] = array();
              }

              If you want to clear the $SESSION array completely
              you can use

              $_SESSION = array();
              
              // and maybe
              unset($_SESSION);
                $_SESSION['cart'][] = $itemid;
                /*
                i need something that will delete an id out of the array so the cart wont display this.
                
                this is what i tried but nothing was change but no errors occured,
                */
                
                $id=$_POST['id'];
                unset($_SESSION['cart'][$id]);

                if the array $cart has not specifically numbered elements, (keys by the name of 'id')
                it takes a loop to find item with id=x, in the value of element

                You maybe should use this

                $_SESSION['cart'][$itemid] = $itemid;

                  hi, thanks for all your help but i still cant get it working... here is what i have

                  Here is the add item to session - this works and the cart displays the contents

                  <?php
                  session_start();
                  
                  if (count($_SESSION["cart"]) < 1) {
                      $_SESSION["cart"] = array();
                  }
                  
                  $_SESSION["cart"][] = $_POST["itemid"];
                  
                  
                  header("location:cart.php");
                  
                  ?>
                  

                  the carts remove item button

                  <form id="id" name="id" method="post" action="/remove_item.php">
                         <input name="id" type="hidden" id="id" value="<? echo $info['id']; ?>" />
                         <input name="Submit" type="submit" class="formbox" value="Remove" />
                  </form> 

                  Then the php script that it posts to is...

                  <?php 
                  $_SESSION['cart'][] = $itemid; 
                  $id=$_POST['id']; 
                  unset($_SESSION['cart'][$id]); 
                  header("location:cart.php");
                  
                  ?>
                  

                  though no error is displayed and the script is receiving the id from the form???

                    3 years later

                    Hi I know this is an old thread now but I have the exact same problem and it is driving me insane.

                    I use the unset() function to remove an item in a multidimensional array stored in a session variable.

                    I have used this exact same method before and it works floorlessly but not here.

                    Here is my code:

                    if (isset($_POST['update_check'])) {
                    
                    for ($i=1; $i < $_POST['checkout_total_items']; $i++) {
                    
                    	if ($_POST['checkout_remove_item_'.$i] != '' || $_POST['checkout_quant_'.$i] == 0 || $_POST['checkout_quant_'.$i] == '0') {
                    
                    		//echo 'unset: ';
                    		$remove_which_id = $_POST['checkout_item_id_'.$i];
                    		//print_r($_SESSION['cart_elep'][$_POST['checkout_item_id_'.$i]]);
                    		unset($_SESSION['cart_elep'][$remove_which_id]);
                    
                    	}
                    
                    	$_SESSION['cart_elep'][$_POST['checkout_item_id_'.$i]]['quant'] = $_POST['checkout_quant_'.$i];
                    	$_SESSION['cart_elep'][$_POST['checkout_item_id_'.$i]]['price'] = $_POST['checkout_item_price_'.$i];
                    
                    }

                    It all works perfectly exept the unset() funtion, and I have no idea why.

                    Any help is greatly appreciated

                      Are you sure that the elements of your if statement all return true?

                      If you add a line within the if conditional area such as "echo 'If has returned true!';" - do you get the expected result?

                      What happens if you run the specific code on its own...

                      <?php
                      session_start();
                      
                      echo "<pre>"; print_r($_SESSION['cart_elep']); echo "</pre>";
                      
                      $remove_which_id = ""; // ID to remove
                      unset($_SESSION['cart_elep'][$remove_which_id]); 
                      
                      echo "<pre>"; print_r($_SESSION['cart_elep']); echo "</pre>";
                      ?>
                      

                      Are both array prints the same?

                        Thanks benracer for the quick relpy - dam that was fast!

                        I solved the problem, literally seconds after my post - I feel like such a fool.

                        Basically the unset function was working, but straight after the unset() my code was simply creating the exact same array key and element again.

                        Here is my new code:

                        if (isset($_POST['update_check'])) {
                        
                        for ($i=1; $i < $_POST['checkout_total_items']; $i++) {
                        
                        	if ($_POST['checkout_remove_item_'.$i] != '' || $_POST['checkout_quant_'.$i] == 0 || $_POST['checkout_quant_'.$i] == '0') {
                        
                        		//echo 'unset: ';
                        		$remove_which_id = $_POST['checkout_item_id_'.$i];
                        		//reset($_SESSION['cart']);
                        		unset($_SESSION['cart_elep'][$remove_which_id]);
                        		//print_r($_SESSION['cart_elep']);
                        
                        	} else { // <--- I just added this else in here!!!
                        
                        	$_SESSION['cart_elep'][$_POST['checkout_item_id_'.$i]]['quant'] = $_POST['checkout_quant_'.$i];
                        	$_SESSION['cart_elep'][$_POST['checkout_item_id_'.$i]]['price'] = $_POST['checkout_item_price_'.$i];
                        
                        	}
                        
                        }

                        I just added that else in, can't believe I missed that!

                        I used print_r to display the array immediatly after the unset() and realised what was happening.

                        Thanks anyway.

                          8 years later

                          As HTTP is state protocol.To maintain states on server and share data across multiple pages PHP session are used.PHP sessions are simple way to store data for individual users/client against a unique session ID.Session IDs are normally sent to the browser via session cookies and the ID is used to retrieve existing session data,if session id is not present on server PHP creates a new session, and generate a new session ID.
                          <?php
                          //starting a session
                          session_start();
                          //Creating a session
                          $_SESSION['user_info']=['user_id'=>1,'first_name'=>'php','last_name'=>'scots','status'=>'active'];

                          //checking session
                          if(isset($_SESSION['user_info'])){
                          echo "logged In";
                          }

                          //un setting remove a value from session
                          unset($_SESSION['user_info']['first_name']);

                          // destroying complete session
                          session_destroy();

                          ?>

                            Write a Reply...