How to clear URL variables in php

1.5k views Asked by At
if (isset($_GET['add']) && isset($_GET['price']) && isset($_GET['qty'])){

    $ITEM = array(
      //Item name       
      'name' => $_GET['add'], 
      //Item Price
      'price' => $_GET['price'], 
      //Qty wanted of item
      'qty' => $_GET['qty']     
         );
     $_SESSION['SHOPPING_CART'][] =  $ITEM;

An item is added in cart when page is refresh. any one help how to remove url variable??

2

There are 2 answers

0
Abraham Covelo On BEST ANSWER

As Alex mentions from a http protocol perspective you should use POST requests to update your cart.

Keep in mind that the problem persists using GET or POST request. POST are even more anoying because and alert is displayed to the customer.

To avoid that refreshing the page in the browser the shopping cart gets another duplicated item you can choose for example :

  • Sends a redirection (301,302) after adding the item to the browser to clean the vars from the url/request
  • Use AJAX to add items to the shopping cart
7
alex On

A POST request is designed to update the server's state.

A GET request is not.

You are using the wrong tool for the job. Adding an item to your shopping cart should be a POST request.