如何销毁 PHP 中的特定会话变量? [英] How do I destroy a specific session variable in PHP?

查看:41
本文介绍了如何销毁 PHP 中的特定会话变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在研究购物车系统.它需要用户登录才能访问购物车.因此,如果用户未登录,我编写了一些代码来禁用对购物车页面的访问.但是,每当我尝试清空购物车时,我都会退出.我只想销毁购物车会话而不是用户会话.这是我的代码:

对于购物车页面:

alert('您必须登录.');window.location.href='index.php#login'";}?><?php包括('../import/layout.php');?><身体><div class="site-wrapper" id="index"><div class="site-wrapper-inner"><div class="cover-container"><?php包括('../import/nav-two.php');?><!-- <div class="内盖">

<div class="mastfoot"><div class="inner"><p>&copy;2015 香香鸡舍餐厅,版权所有.<a class="menu-item pull-right" href="#index">返回顶部</a></p>

-->

<div id="购物车"><div class="容器"><?php包括 ('../cart/index.php');?>

对于购物车更新:

query("SELECT product_name,price FROM products WHERE product_code='$product_code' LIMIT 1");$obj = $results->fetch_object();if ($results) {//我们有产品信息//为会话变量准备数组$new_product = array(array('name'=>$obj->product_name, 'code'=>$product_code, 'qty'=>$product_qty, 'price'=>$obj->price));if(isset($_SESSION["products"]))//如果我们有会话{$发现=假;//将找到的项目设置为falseforeach ($_SESSION["products"] as $cart_itm)//遍历会话数组{if($cart_itm["code"] == $product_code){//该项目存在于数组中$product[] = array('name'=>$cart_itm["name"], 'code'=>$cart_itm["code"], 'qty'=>$product_qty, 'price'=>$cart_itm["价格"]);$发现=真;}别的{//项目不​​存在于列表中,只需检索旧信息并为会话变量准备数组$product[] = array('name'=>$cart_itm["name"], 'code'=>$cart_itm["code"], 'qty'=>$cart_itm["qty"], 'price'=>$cart_itm["price"]);}}if($found == false)//我们没有在数组中找到项目{//在数组中添加新的用户项$_SESSION["products"] = array_merge($product, $new_product);}别的{//在数组列表中找到用户项,并增加数量$_SESSION["products"] = $product;}}别的{//如果不存在则创建一个新的会话变量$_SESSION["products"] = $new_product;}}//重定向回原页面header('位置:'.$return_url);}//从购物车中移除商品if(isset($_GET["removep"]) &&isset($_GET["return_url"]) &&isset($_SESSION["products"])){$product_code = $_GET["removep"];//获取要删除的产品代码$return_url = base64_decode($_GET["return_url"]);//获取返回地址foreach ($_SESSION["products"] as $cart_itm)//遍历会话数组 var{if($cart_itm["code"]!=$product_code){//item确实存在,t存在于列表中$product[] = array('name'=>$cart_itm["name"], 'code'=>$cart_itm["code"], 'qty'=>$cart_itm["qty"], 'price'=>$cart_itm["price"]);}//为购物车创建一个新的产品列表$_SESSION["products"] = $product;}//重定向回原页面header('位置:'.$return_url);}?>

解决方案

怎么样

unset($_SESSION["products"])

代替

session_destroy()

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

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

解决方案

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).

这篇关于如何销毁 PHP 中的特定会话变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
PHP最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆