How to destroy a specific session variable in PHP?

I am currently working on a shopping cart system. To enter the cart requires a user login. Therefore, I wrote several codes to disable access to the cart page if the user was not logged in. However, when I try to empty the trash, I exit the system. I just want to destroy the cart session, not the user session. Here is my code:

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

To update the basket:

 <?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); } ?> 
+7
source share
9 answers

What about

 unset($_SESSION["products"]) 

instead

 session_destroy() 

There is only one session for a user. Thus, there is no way to destroy a โ€œspecificโ€ session. What you can do is delete the contents of the session that is responsible for displaying the recycle bin (as shown above).

+30
source

you can use

  unset($_SESSION["products"]); 
+3
source

Use,

 unset($_SESSION["products"]); 

session_destroy() destroy all sessions, while the above line will destroy a specific session variable.

+3
source

You want to not destroy the session, since you want the user to log in. The best way to do this is to delete or rewrite your basket variables as needed. You can either unset($_SESSION['products']); completely remove the variable, or $_SESSION['products'] = array(); reset to an empty trash.

At some point (if you save the basket later in the database), you can use the same code as when deleting an item from the basket for all the elements present in it ...

+3
source

Use unset() for all session variables specific to any site 1 or 2.

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

unset() func is useful in this case.

session_destroy() func will destroy

+1
source

session_destroy() destroys the entire session variable and unset(session variable) destroys the specific session variable.

+1
source

The specific session value can be set to "null", and then check this session value using the empty () function, where necessary. Or for a certain session value, some let say 0 value can be set, and then check the session value with the given value for performing some operations.

+1
source

use unset () instead of session_destroy (). where unset indicate a specific variable, where session_destroy destroys all session variables.

 unset($_SESSION["products"]) 
+1
source

Source: https://habr.com/ru/post/982725/


All Articles