title: How do I destroy a specific session variable in PHP?
body: <p>I'm currently working on a shopping cart system. It requires a user login to access the cart. So I've wrote some codes to disable access of the cart page if the user is not logged in. However, whenever I try to empty the cart, I get logged out. I just want to destroy the cart session and not the user session. Here's my code:</p>
  
  <p>For the cart page:</p>
  
  <pre><code>&lt;?php
    session_start();
    if(isset($_SESSION['userID'])){
  
    }
    elseif(!isset($_SESSION['userID'])){
        echo 
        "&lt;script&gt;
          alert('You must be logged in.');
          window.location.href='index.php#login'
        &lt;/script&gt;";
      }
  ?&gt;
  
    &lt;?php
      include ('../import/layout.php');
    ?&gt;
  
    &lt;body&gt;
  
      &lt;div class="site-wrapper" id="index"&gt;
  
        &lt;div class="site-wrapper-inner"&gt;
  
          &lt;div class="cover-container"&gt;
  
            &lt;?php
              include ('../import/nav-two.php');
            ?&gt;
  
            &lt;!-- &lt;div class="inner cover"&gt;
  
            &lt;/div&gt;
  
            &lt;div class="mastfoot"&gt;
              &lt;div class="inner"&gt;
                &lt;p&gt;&amp;copy; 2015 Aroma Chicken House Restaurant, All Rights Reserved.
                   &lt;a class="menu-item pull-right" href="#index"&gt;Back to Top&lt;/a&gt;
                &lt;/p&gt;
              &lt;/div&gt;
            &lt;/div&gt; --&gt;
  
          &lt;/div&gt;
  
          &lt;div id="cart"&gt;
            &lt;div class="container"&gt;
              &lt;?php
                include ('../cart/index.php');
              ?&gt;
            &lt;/div&gt;
          &lt;/div&gt;
  
  
        &lt;/div&gt;
  
      &lt;/div&gt;
  
  
    &lt;/body&gt;
  </code></pre>
  
  <p>For the cart update:</p>
  
  <pre><code>&lt;?php
  session_start();
  include_once("config/config.php");
  
  //empty cart by distroying current session
  if(isset($_GET["emptycart"]) &amp;&amp; $_GET["emptycart"]==1)
  {
      $return_url = base64_decode($_GET["return_url"]); //return url
      session_destroy();
      header('Location:'.$return_url);
  }
  
  //add item in shopping cart
  if(isset($_POST["type"]) &amp;&amp; $_POST["type"]=='add')
  {
      $product_code   = filter_var($_POST["product_code"], FILTER_SANITIZE_STRING); //product code
      $product_qty    = filter_var($_POST["product_qty"], FILTER_SANITIZE_NUMBER_INT); //product code
      $return_url     = base64_decode($_POST["return_url"]); //return url
  
      //MySqli query - get details of item from db using product code
      $results = $mysqli-&gt;query("SELECT product_name,price FROM products WHERE product_code='$product_code' LIMIT 1");
      $obj = $results-&gt;fetch_object();
  
      if ($results) { //we have the product info 
  
          //prepare array for the session variable
          $new_product = array(array('name'=&gt;$obj-&gt;product_name, 'code'=&gt;$product_code, 'qty'=&gt;$product_qty, 'price'=&gt;$obj-&gt;price));
  
          if(isset($_SESSION["products"])) //if we have the session
          {
              $found = false; //set found item to false
  
              foreach ($_SESSION["products"] as $cart_itm) //loop through session array
              {
                  if($cart_itm["code"] == $product_code){ //the item exist in array
  
                      $product[] = array('name'=&gt;$cart_itm["name"], 'code'=&gt;$cart_itm["code"], 'qty'=&gt;$product_qty, 'price'=&gt;$cart_itm["price"]);
                      $found = true;
                  }else{
                      //item doesn't exist in the list, just retrive old info and prepare array for session var
                      $product[] = array('name'=&gt;$cart_itm["name"], 'code'=&gt;$cart_itm["code"], 'qty'=&gt;$cart_itm["qty"], 'price'=&gt;$cart_itm["price"]);
                  }
              }
  
              if($found == false) //we didn't find item in array
              {
                  //add new user item in array
                  $_SESSION["products"] = array_merge($product, $new_product);
              }else{
                  //found user item in array list, and increased the quantity
                  $_SESSION["products"] = $product;
              }
  
          }else{
              //create a new session var if does not exist
              $_SESSION["products"] = $new_product;
          }
  
      }
  
      //redirect back to original page
      header('Location:'.$return_url);
  }
  
  //remove item from shopping cart
  if(isset($_GET["removep"]) &amp;&amp; isset($_GET["return_url"]) &amp;&amp; isset($_SESSION["products"]))
  {
      $product_code   = $_GET["removep"]; //get the product code to remove
      $return_url     = base64_decode($_GET["return_url"]); //get return url
  
  
      foreach ($_SESSION["products"] as $cart_itm) //loop through session array var
      {
          if($cart_itm["code"]!=$product_code){ //item does,t exist in the list
              $product[] = array('name'=&gt;$cart_itm["name"], 'code'=&gt;$cart_itm["code"], 'qty'=&gt;$cart_itm["qty"], 'price'=&gt;$cart_itm["price"]);
          }
  
          //create a new product list for cart
          $_SESSION["products"] = $product;
      }
  
      //redirect back to original page
      header('Location:'.$return_url);
  }
  ?&gt;
  </code></pre>