How do I destroy a specific session variable in PHP?

Benedict Payot

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:

For the cart page:

<?php
  session_start();
  if(isset($_SESSION['userID'])){

  }
  elseif(!isset($_SESSION['userID'])){
      echo 
      "<script>
        alert('You must be logged in.');
        window.location.href='index.php#login'
      </script>";
    }
?>

  <?php
    include ('../import/layout.php');
  ?>

  <body>

    <div class="site-wrapper" id="index">

      <div class="site-wrapper-inner">

        <div class="cover-container">

          <?php
            include ('../import/nav-two.php');
          ?>

          <!-- <div class="inner cover">

          </div>

          <div class="mastfoot">
            <div class="inner">
              <p>&copy; 2015 Aroma Chicken House Restaurant, All Rights Reserved.
                 <a class="menu-item pull-right" href="#index">Back to Top</a>
              </p>
            </div>
          </div> -->

        </div>

        <div id="cart">
          <div class="container">
            <?php
              include ('../cart/index.php');
            ?>
          </div>
        </div>


      </div>

    </div>


  </body>

For the cart update:

<?php
session_start();
include_once("config/config.php");

//empty cart by distroying current session
if(isset($_GET["emptycart"]) && $_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"]) && $_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->query("SELECT product_name,price FROM products WHERE product_code='$product_code' LIMIT 1");
    $obj = $results->fetch_object();

    if ($results) { //we have the product info 

        //prepare array for the session variable
        $new_product = array(array('name'=>$obj->product_name, 'code'=>$product_code, 'qty'=>$product_qty, 'price'=>$obj->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'=>$cart_itm["name"], 'code'=>$cart_itm["code"], 'qty'=>$product_qty, 'price'=>$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'=>$cart_itm["name"], 'code'=>$cart_itm["code"], 'qty'=>$cart_itm["qty"], 'price'=>$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"]) && isset($_GET["return_url"]) && 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'=>$cart_itm["name"], 'code'=>$cart_itm["code"], 'qty'=>$cart_itm["qty"], 'price'=>$cart_itm["price"]);
        }

        //create a new product list for cart
        $_SESSION["products"] = $product;
    }

    //redirect back to original page
    header('Location:'.$return_url);
}
?>
Markus Müller

What about

unset($_SESSION["products"])

instead of the

session_destroy()

There is only one session per user. So there is no way to destroy a "specific" session. What you can do is delete the contents of your session responsible for the display of the cart (as shown above).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do i destroy session of a particular user using Id?

From Dev

How do i destroy session of a particular user using Id?

From Dev

What does session_destroy() do in PHP?

From Dev

How do i pass php variable inside a row cell to a $_SESSION variable

From Dev

How do I check if a session variable is set?

From Dev

How do I pass a session variable correctly?

From Dev

How do I pass a session variable correctly?

From Dev

Destroy a specific session in codeigniter

From Dev

how do i give session email to php?

From Dev

How Do I Destroy an SKPhysicsBody?

From Dev

Destroy session in php

From Dev

Destroy session in php

From Dev

session destroy in php

From Dev

PHP or Javascript - How to Destroy a Website In specific time

From Dev

How do I implement session aware WEB API on specific controllers?

From Dev

How to destroy the session in codeigniter

From Dev

How do I delete a specific array in php

From Dev

How do I create a session variable in DotVVM viewmodel?

From Dev

How do I deal with AWS and Session variable in Meteor?

From Dev

How do I set a user environment variable? (permanently, not session)

From Dev

How do I set the same session variable for all views programmatically?

From Dev

How can I access my session variable in a different page with php?

From Dev

How can I use a PHP Session variable in a dynamically loaded navbar?

From Dev

Apache Cordova - How do we protect the page after session destroy

From Dev

PHP session unset or PHP session destroy

From Dev

how to destroy session and cookies from same server in php?

From Dev

getting session variable even after session destroy

From Dev

How do I get this cookie into a PHP variable?

From Dev

Fail to destroy session and cookies in PHP

Related Related

  1. 1

    How do i destroy session of a particular user using Id?

  2. 2

    How do i destroy session of a particular user using Id?

  3. 3

    What does session_destroy() do in PHP?

  4. 4

    How do i pass php variable inside a row cell to a $_SESSION variable

  5. 5

    How do I check if a session variable is set?

  6. 6

    How do I pass a session variable correctly?

  7. 7

    How do I pass a session variable correctly?

  8. 8

    Destroy a specific session in codeigniter

  9. 9

    how do i give session email to php?

  10. 10

    How Do I Destroy an SKPhysicsBody?

  11. 11

    Destroy session in php

  12. 12

    Destroy session in php

  13. 13

    session destroy in php

  14. 14

    PHP or Javascript - How to Destroy a Website In specific time

  15. 15

    How do I implement session aware WEB API on specific controllers?

  16. 16

    How to destroy the session in codeigniter

  17. 17

    How do I delete a specific array in php

  18. 18

    How do I create a session variable in DotVVM viewmodel?

  19. 19

    How do I deal with AWS and Session variable in Meteor?

  20. 20

    How do I set a user environment variable? (permanently, not session)

  21. 21

    How do I set the same session variable for all views programmatically?

  22. 22

    How can I access my session variable in a different page with php?

  23. 23

    How can I use a PHP Session variable in a dynamically loaded navbar?

  24. 24

    Apache Cordova - How do we protect the page after session destroy

  25. 25

    PHP session unset or PHP session destroy

  26. 26

    how to destroy session and cookies from same server in php?

  27. 27

    getting session variable even after session destroy

  28. 28

    How do I get this cookie into a PHP variable?

  29. 29

    Fail to destroy session and cookies in PHP

HotTag

Archive