php - 如何销毁 PHP 中的特定 session 变量?

标签 php session

我目前正在研究购物车系统。它需要用户登录才能访问购物车。所以我写了一些代码来禁止用户未登录时访问购物车页面。但是,每当我尝试清空购物车时,我都会被注销。我只想销毁购物车 session 而不是用户 session 。这是我的代码:

对于购物车页面:

<?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>

对于购物车更新:

<?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);
}
?>

最佳答案

关于

unset($_SESSION["products"])

而不是

session_destroy()

每个用户只有一个 session 。所以没有办法销毁“特定” session 。您可以做的是删除负责显示购物车的 session 内容(如上所示)。

关于php - 如何销毁 PHP 中的特定 session 变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28602173/

相关文章:

javascript - 格式化curl返回数据

php - 如何从 PHP session 数组中删除变量

php - 在 MySQL 中搜索 - 分隔列

php - 我应该如何将数组值存储在 MySQL 数据库中?

php - 对于只需要一个连接的应用程序,在 PHP5 中隐藏数据库连接代码的最佳方法是什么?

php - zend 框架中一个模块中的多个 Controller

asp.net - 访问 ActionFilterAttribute 中的 Session.SessionID

php - 从 session ID 为 0 的购物车中删除项目

php - 数组作为 session 变量

php - 清理 php session 文件