Jump to content

Leave page 'destroy_session'?


PapaBurgundy

Recommended Posts

Hi

 

I'm currently in the process of making a small online shop.

 

At the moment I only want the site to function by allowing the user to purchase one product.

 

It all works fine apart from one feature.

 

The user could have one product in their cart, but then click back onto the products page and order another product; this would put the price up but only store one product in the database.

 

Is there a way to destroy the cart session with the user leaving the page? Or either some sort of function which stops the user from leaving the page until they clear their cart?

 

My code to delete a product from the cart

//delete item from cart
if(isset($_GET['delete'])){
	$_SESSION['cart_'.(int)$_GET['delete']]='0';
	header('Location: order.php');
}

 

My first idea was to take this code and somehow relate or apply it to every link, but I'm guessing there's an easier way around that. The error message would make more sense to me but I can't seem to find anything anywhere.

 

Thanks!

Link to comment
Share on other sites

I would recommend storing only the product ID, and retrieve the price from the database

I assume every product has an ID and the order page, which set this product to purchase

The order page has to set the purchased product ID with the new ID not the existing one, in case the user had returned and picked another product

 

Link to comment
Share on other sites

if(isset($_GET['add'])){
	$_SESSION['repair_'.(int)$_GET['add']]+='1'; //if the 'add' is set then create a session named with the id used
	header('Location: order.php');
}

 

This is the code I'm using to add a product to the cart.

 

I've tried some variations but it doesn't seem to do what I want it to as it's already set up to handle more data.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.