如何破坏PHP特定的会话变量?

问题描述 投票:7回答:9

我目前工作的一个购物车系统。它要求用户登录访问的车。所以我写了一些代码,如果用户没有。但是,每当我试图清空购物车,我得到注销登录禁用车页面的访问。我只是想摧毁车会话,而不是用户会话。这里是我的代码:

对于购物车页面:

<?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);
}
?>
php session
9个回答
32
投票

关于什么

unset($_SESSION["products"])

而不是

session_destroy()

有每个用户只有一个会话。因此,有没有办法摧毁一个“特定”会话。你可以做的就是删除负责购物车的显示会话的内容(如上图所示)。


3
投票

您可以使用

  unset($_SESSION["products"]);

3
投票

采用,

unset($_SESSION["products"]);

session_destroy()会破坏所有的会议,但上面的行会毁掉一个特定的会话变量。


2
投票

你想要的是不破坏会议,只要你想保持登录的用户。要做到这一点是通过去除或根据需要覆盖您的购物车的变量的最好方法。您可以unset($_SESSION['products']);完全删除变量,或$_SESSION['products'] = array();其重置为一个空的购物车。

在某些时候(如果保存在数据库中的车后),你可能想为你做的所有其中存在的物品去除购物车内的商品时使用相同的代码...


1
投票

使用unset()所有特定会话变量站点1或2。

unset($_SESSION['var1']);
//or
unset($_SESSION['var2']);

1
投票

unset() FUNC在这种情况下非常有用。

session_destroy() FUNC会破坏


0
投票

session_destroy()是摧毁所有会话变量和unset(session variable)摧毁特定的会话变量。


0
投票

具体会话的值可以设置为“空”,然后检查用在需要的地方空()函数或特定会话的值可以设置为某个值会话的价值让说0,然后检查会话与设定值值进行一些操作。


0
投票

使用未设置(),而不是session_destroy()。其中,未设置点的特定变量,分别session_destroy消灭所有的会话变量。

unset($_SESSION["products"])
© www.soinside.com 2019 - 2024. All rights reserved.