Connecting Tech Pros Worldwide Forums | Help | Site Map

add item to shopping cart problem

Josef Blösl
Guest
 
Posts: n/a
#1: Jul 21 '05
hi folks!

i have a cart link in all of my listed products on the site. when i
press this link i will generate a url like this

http://www.mysite.com/index.htm?loca...rt&productid=3

after i press this link the site cart.php will be included and add the
wanted product to the cart, and show the cart.

my problem now is after the cart is showed with the new product in it,
when i press the F5 key in the browser to refresh the site, the same
product will added to the cart once more, because the previous generated
url is unchanged in the browser.

have anyone a idea how to fix this problem?

thanx...

Colin McKinnon
Guest
 
Posts: n/a
#2: Jul 21 '05

re: add item to shopping cart problem


Josef Blösl wrote:
[color=blue]
> hi folks!
>
> i have a cart link in all of my listed products on the site. when i
> press this link i will generate a url like this
>
>[/color]
http://www.mysite.com/index.htm?loca...rt&productid=3[color=blue]
>
> after i press this link the site cart.php will be included and add the
> wanted product to the cart, and show the cart.
>
> my problem now is after the cart is showed with the new product in it,
> when i press the F5 key in the browser to refresh the site, the same
> product will added to the cart once more, because the previous generated
> url is unchanged in the browser.
>
> have anyone a idea how to fix this problem?
>
> thanx...[/color]

Yes - attach a random cookie (not a web cookie) to each request and ignore
requests where the cookie is already in the basket.

yawn.

C.
Josef Blösl
Guest
 
Posts: n/a
#3: Jul 21 '05

re: add item to shopping cart problem


Colin McKinnon schrieb:[color=blue]
> Josef Blösl wrote:
>
>[color=green]
>>hi folks!
>>
>>i have a cart link in all of my listed products on the site. when i
>>press this link i will generate a url like this
>>
>>[/color]
>
> http://www.mysite.com/index.htm?loca...rt&productid=3
>[color=green]
>>after i press this link the site cart.php will be included and add the
>>wanted product to the cart, and show the cart.
>>
>>my problem now is after the cart is showed with the new product in it,
>>when i press the F5 key in the browser to refresh the site, the same
>>product will added to the cart once more, because the previous generated
>>url is unchanged in the browser.
>>
>>have anyone a idea how to fix this problem?
>>
>>thanx...[/color]
>
>
> Yes - attach a random cookie (not a web cookie) to each request and ignore
> requests where the cookie is already in the basket.
>
> yawn.
>
> C.[/color]

thanks a lot... good idea
dracolytch
Guest
 
Posts: n/a
#4: Jul 21 '05

re: add item to shopping cart problem


Colin,
If you're so bored, then why not give an answer that might help him
improve his code, instead of just dodge a bug.

Josef,
Ultimately, your underlying problem is that you have an "action"
variable. You need to be separating out your action code from your
display code.

Think of each page in PHP as a method in an object. Instead of having
one page, and choosing the behavior based on a switch statement, take
the time to separate out actions to dedicated pages.

In this case, create a cart-additem.php page and a cart-display.php
page. Have cart-additem.php do variable/access validation, and then add
the item to the cart. Then, relocate to the display page.

You will find that this code will be easier to maintain, and will have
few/no state-related bugs.

~D

dracolytch
Guest
 
Posts: n/a
#5: Jul 21 '05

re: add item to shopping cart problem


Colin,
If you're so bored, then why not give an answer that might help him
improve his code, instead of just dodge a bug.

Josef,
Ultimately, your underlying problem is that you have an "action"
variable. You need to be separating out your action code from your
display code.

Think of each page in PHP as a method in an object. Instead of having
one page, and choosing the behavior based on a switch statement, take
the time to separate out actions to dedicated pages.

In this case, create a cart-additem.php page and a cart-display.php
page. Have cart-additem.php do variable/access validation, and then add
the item to the cart. Then, relocate to the display page.

You will find that this code will be easier to maintain, and will have
few/no state-related bugs.

~D

dracolytch
Guest
 
Posts: n/a
#6: Jul 21 '05

re: add item to shopping cart problem


You need to separate your action code from your display code. You
should never have an action variable or mode variable. Ever.

Each page in PHP is like a function or method in other languages. Each
one should have a unique, specific purpose.

Create a link that goes to the cart-additem.php page.
Have cart-additem.php validate your variables, add the item to the
cart, and then relocate to a cart-display.php page.

Writing your own shopping cart can be a real dangerous thing to do.
Crackers often see custom carts as ripe targets for attack. Harden,
harden, harden your code.

~D

Josef Blösl
Guest
 
Posts: n/a
#7: Jul 21 '05

re: add item to shopping cart problem


dracolytch schrieb:[color=blue]
> You need to separate your action code from your display code. You
> should never have an action variable or mode variable. Ever.
>
> Each page in PHP is like a function or method in other languages. Each
> one should have a unique, specific purpose.
>
> Create a link that goes to the cart-additem.php page.
> Have cart-additem.php validate your variables, add the item to the
> cart, and then relocate to a cart-display.php page.
>
> Writing your own shopping cart can be a real dangerous thing to do.
> Crackers often see custom carts as ripe targets for attack. Harden,
> harden, harden your code.
>
> ~D
>[/color]

first thank you for your post!

i understand what you mean, but how can i say (inside my addtocart.php
script) to the browser he should automatically switch to the next page
(showcart.php)
dracolytch
Guest
 
Posts: n/a
#8: Jul 21 '05

re: add item to shopping cart problem


Sorry about the multiple posts... My 'net connection at work keeps
timing out, and leads me to believe responses didn't post. Ugh.

~D

dracolytch
Guest
 
Posts: n/a
#9: Jul 21 '05

re: add item to shopping cart problem


Oh, here's the redirect code you're looking for:

header('location: showcart.php');
die();

The die(); at the end ensures termination of the current page. This is
especially useful if you're doing validation checking, and found a
problem. This will redirect you to a new page, and prevent anything
else in the current page from being executed.

~D

JDS
Guest
 
Posts: n/a
#10: Jul 21 '05

re: add item to shopping cart problem


On Tue, 19 Jul 2005 14:28:35 +0200, Josef Blösl wrote:
[color=blue]
> have anyone a idea how to fix this problem?[/color]

There are several ways.

One is to include some sort of tracking variable and only do the update if
the tracking variable is flagged correctly.

Another, and the way I would do it, is to use a different PHP script
entirely to do the actual form processing, and send the browser to the
correct "results" page using header("Location: blah.php");

Thus you would have an invisible processing-only PHP script that redirects
the user on completion to the proper results page.

I hope this makes sense.

--
JDS | jeffrey@example.invalid
| http://www.newtnotes.com
DJMBS | http://newtnotes.com/doctor-jeff-master-brainsurgeon/

R. Rajesh Jeba Anbiah
Guest
 
Posts: n/a
#11: Jul 21 '05

re: add item to shopping cart problem




Josef Blösl wrote:[color=blue]
> hi folks!
>
> i have a cart link in all of my listed products on the site. when i
> press this link i will generate a url like this
>
> http://www.mysite.com/index.htm?loca...rt&productid=3
>
> after i press this link the site cart.php will be included and add the
> wanted product to the cart, and show the cart.
>
> my problem now is after the cart is showed with the new product in it,
> when i press the F5 key in the browser to refresh the site, the same
> product will added to the cart once more, because the previous generated
> url is unchanged in the browser.
>
> have anyone a idea how to fix this problem?[/color]

This is not acutally a problem. Moreover it's a usability feature.
If it's get added, the enduser may delete it.

--
<?php echo 'Just another PHP saint'; ?>
Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com

Tony
Guest
 
Posts: n/a
#12: Jul 21 '05

re: add item to shopping cart problem


R. Rajesh Jeba Anbiah wrote:[color=blue]
> Josef Blösl wrote:[color=green]
>> hi folks!
>>
>> i have a cart link in all of my listed products on the site. when i
>> press this link i will generate a url like this
>>
>> http://www.mysite.com/index.htm?loca...rt&productid=3
>>
>> after i press this link the site cart.php will be included and add
>> the wanted product to the cart, and show the cart.
>>
>> my problem now is after the cart is showed with the new product in
>> it, when i press the F5 key in the browser to refresh the site, the
>> same product will added to the cart once more, because the previous
>> generated url is unchanged in the browser.
>>
>> have anyone a idea how to fix this problem?[/color]
>
> This is not acutally a problem. Moreover it's a usability feature.
> If it's get added, the enduser may delete it.[/color]

Actually, it's easily solvable. You have two scripts - let's call them
additem.php and showcart.php

when you add an item to the cart, it calls additem.php, which does the
actual work of putting the item in the cart. But it doesn't output anything.
When it's done processing the added item, additem.php uses header() to call
showcart.php, which displays the cart. If you hit refresh at this point,
you're only refreshing showcart.php, and not processing the added item
again.

--
Tony Garcia
Web Right! Development
Riverside, CA
www.WebRightDevelopment.com


richard
Guest
 
Posts: n/a
#13: Jul 30 '05

re: add item to shopping cart problem


On 2005-07-19, Josef Blösl <josef.bloesl@gmx.at> wrote:[color=blue]
> hi folks!
>
> i have a cart link in all of my listed products on the site. when i
> press this link i will generate a url like this
>
> http://www.mysite.com/index.htm?loca...rt&productid=3
>
> after i press this link the site cart.php will be included and add the
> wanted product to the cart, and show the cart.
>
> my problem now is after the cart is showed with the new product in it,
> when i press the F5 key in the browser to refresh the site, the same
> product will added to the cart once more, because the previous generated
> url is unchanged in the browser.
>
> have anyone a idea how to fix this problem?
>
> thanx...[/color]

This is a common problem. The usual thing to do is send a timestamp with
the data. When you recieve a form post or vars in the get string and the
timestamp is present store all the data in the session and if you
receive the exact same data with the exact same information, then you
have a duplicate and ignore, if you have the same timestamp and
different form values you unde the first post and do the second post
instad. Or question the user and ask them to fill in the form again so
you can be sure.

Don't forget to expire info from the session if it starts getting too
large
Closed Thread