PapaBurgundy Posted May 3, 2011 Share Posted May 3, 2011 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! Quote Link to comment https://forums.phpfreaks.com/topic/235447-leave-page-destroy_session/ Share on other sites More sharing options...
requinix Posted May 3, 2011 Share Posted May 3, 2011 Only one product at a time? Ouch. Modify whatever the "add to cart" code does so that it only adds if the cart is empty. Quote Link to comment https://forums.phpfreaks.com/topic/235447-leave-page-destroy_session/#findComment-1210072 Share on other sites More sharing options...
BloodyMind Posted May 3, 2011 Share Posted May 3, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/235447-leave-page-destroy_session/#findComment-1210148 Share on other sites More sharing options...
PapaBurgundy Posted May 4, 2011 Author Share Posted May 4, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/235447-leave-page-destroy_session/#findComment-1210340 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.