How to store, update and remove data in a php session

Updated: 27-Feb-2022 | Tags: PHP Tutorials | Views: 961


Hello everyone in this article we are gonna see how to create a products session, like the ones we have in a shopping cart. A lot of php shopping cart's applications are using sessions to temporary store data (products) so those data are available across the whole website or application.
In the paragraphs below we are going to create a products session and we will see how to add a product in the session, how to update the quantity of a product, and how to remove a product.
Let's start.

The PHP code

First of all i have to start a session with the session_start() function in line 2.

This is mandatory we can not use sessions if we don't use the session_start() function. To be more precise we have to use the session_start() function in every file in which we are using sessions.

session_start();  // First thing in the file.

$product = [
	"id" => 1,
	"name" => "Product A",
	"price" => 15.99,
	"discount_percentage" => 12,
	"quantity" => 1
  • In line 4 i have a an associative array named $product which is holding the product that i am going to store in the session.
  • The product has an id, a name, a price, a discount, and a quantity key.
  • An associative array is structured in key => value pairs.

Adding the product to the session

Let's see how we can add the product in a session. But first let me describe the logic that i use.

I want to write the code in a way that we do a check on the incoming product. And if the product don't exists inside our session array, only then we are going to add it.
To achieve that i am going to check the incoming product's id against all products ids stored in the session.
I am doing this because i don't want the same product twice(or more times) stored in the session.

	$_SESSION['products'][] = $product;
	foreach ($_SESSION['products'] as $stored_product) {
		$ids[] = $stored_product['id'];
	if(!in_array($product['id'], $ids)){
		$_SESSION['products'][] = $product;
  • We are going to name the session that we create "products"
  • In line 12 i will check first if the $_SESSION['products'] exists.
  • In line 13 if the session doesn't exists, i am creating the products session, and i add the product in it. I don't need to check for duplicates because this is the first entry in our session.
  • Now if the session exists, line 15, the first thing that i do is to loop through the products that the session holds, and take all products ids and put them in an array named $ids, in line 16.
  • Next in line 18 i have an if statement and i will check if the incoming product's id is NOT in the $ids array.
    If the condition returns true then i will add the product in the products session in line 19.
  • And in this way i am avoiding any duplicates inside the products session.

When we run the script and do a var_dump($_SESSION['products']) on the products session we get this.

This is what the products session looks like. The first array is the session and the nested array is the product.

array (
  0 => array ('id' => 1, 'name' => 'Product A', 'price' => 15.99, ...)

If you run the file again, nothing is going to happening because we don't accept the same product(based on the id check) inside the session. But if you go to the $product[] array and change the id to two(2), you will see a second product in the session.
Something like this.

We see that the second product is added in the session array. I also changed the name and the price.

array (
  0 => array ('id' => 1, 'name' => 'Product A', 'price' => 15.99, ...),
  1 => array ('id' => 2, 'name' => 'Product B', 'price' => 20.99, ...)

And we can add as many products we like, but every product has to have a unique id.

Update the quantity

Now let's see how we can update the quantity of a product. We are gonna need two pieces of information.

First the id of the product that we want to update, and second the new quantity value.
So in line 24 i have a variable which holds the id of the product that we want to update. And in line 25 i have the new quantity value.

$update_item_with_id = 1;
$quantity = 15;

foreach ($_SESSION['products'] as $key => $stored_product) {
	if($update_item_with_id == $stored_product['id']){
		$_SESSION['products'][$key]['quantity'] = $quantity;
  • In line 27 with a foreach loop i will go through the products session array, and i will access the key and value from every stored product.
    array (
      // keys(0, 1) => values(product, product).
      0 => array ('id' => 1, 'name' => 'Product A', 'price' => 15.99, ...),
      1 => array ('id' => 2, 'name' => 'Product B', 'price' => 20.99, ...)
  • Next in line 28 i have again an if block, an i check a condition where i match the incoming product's id, against every product's id. And if i have a match, i will use the key to target the product's quantity and set the new value in line 29.
  • And that's it if we run the code we will see the product's quantity with id 2 is set to 15.
    That's how we update a value in a session.

Remove a product from the session

Now let's see how we can remove a product from our products session.
The information that we here need is the product's id. Based on the id we are gonna remove the product from the session. In line 33 we have a variable which holds the id (2) of the product that we are going to remove.

$delete_item_with_id = 2;
foreach ($_SESSION['products'] as $key => $stored_product) {
	if($delete_item_with_id == $stored_product['id']){
  • We are gonna use again a foreach loop, line 34, as we did in the update code. And again we are gonna search for the given id in line 35.
  • And if we have a match we use the key to unset the whole product and remove it from the products session.
  • And if we want to unset (delete from memory) the whole session we just do this.


We saw hot to store, update, and remove data in a php session.

Buy me a coffee

If you like to say thanks, you can buy me a coffee.

Buy me a coffee with paypal

Comment section

You can leave a comment, it will help me a lot.

Or you can just say hi. 😉

Tutorial Categories