Forum Moderators: coopster

Message Too Old, No Replies

Removing GET variables from url

         

Polish Renegade

8:00 pm on Aug 1, 2006 (gmt 0)

10+ Year Member



Hi,

I am trying to send information from one page "products.php" to "viewcart.php".

so I use <a href="viewcart.php?id=pd_id_1"> ...
In viewcart.php I take the 'id' variable, push it in a $_SESSION array 'cart' and then create the cart from a SQL database.

The problem is that whenever the client refresh the page, the $_GET['id'] variable is pushed inside the array 'cart' and I end up with multiple items. I want to get rid of the $_GET information in the url but just can't make it work. Tryed unset() but it doesn't work.

If not, is there a way to send $_SESSION variables from a link?
Instead of using [...]viewcart.php?#*$!XX to get something like [...]viewcart.php?$_SESSION['id']="pd_id_1"?

Thanks for the help.

whoisgregg

12:39 am on Aug 2, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



When the $_GET['id'] variable is set, push it into your session cart array and then immediately redirect the user to the "viewcart.php" page without the query string.

So the first few lines of "viewcart.php" might look something like this untested, pseudocode:


if(isset($_GET['id'])){
$_SESSION['cart'][] = $_GET['id']; // you should sanitize that $_GET of course
header("Location: http://www.example.com/viewcart.php"); //strips off the?query=string
}

Now, that's the immediate solution to your problem. However, I suspect this method will still cause problems down the road. What if they want to buy more than one of a product? Are you storing quantity in that session array? If so, then perhaps they are hitting refresh because they don't know how else to add multiple of the same item?

Perhaps just using the product id as the session cart key would be helpful:

$_SESSION['cart'][$_GET['id']] = array( "quantity"=>1, "name"=>'Red Widgets' );

FiRe

9:53 am on Aug 2, 2006 (gmt 0)

10+ Year Member



You could try
unset($_GET['id']);