By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,466 Members | 1,141 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,466 IT Pros & Developers. It's quick & easy.

Back button and get request...

P: n/a
I've made a script for managing shoppingcart (something like
cart.php?action=add&item=12345) and it works fine, the problem is: when user
hits the back button page after he added something in the cart, the add cart
script is rerun and he ends up with having 2 of the same item. Is there a
way to prevent script from running when accessed via back button?

Thanks

Brko
Jul 17 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
B.r.K.o.N.j.A. (th********@inet.hr) wrote:
: I've made a script for managing shoppingcart (something like
: cart.php?action=add&item=12345) and it works fine, the problem is: when user
: hits the back button page after he added something in the cart, the add cart
: script is rerun and he ends up with having 2 of the same item. Is there a
: way to prevent script from running when accessed via back button?

use POST (personally I dislike POST, but this is exactly the sort of
situation for which is is intended).

OR, change your logic. Instead of adding something, your logic can
indicate how many are wanted. If someone reruns the script then it
doesn't matter because the new value of how-many-wanted is simply the same
as the previous value of how-many-wanted.

OR, use a serial number of some kind so that you can detect when a query
is being re-run and not add the item.

OR, use the refer field and don't update unless your form is the refer.
The refer is no good for security, but is very useful as a convenience at
times like this.

OR, use cookies in conjunction with the request. You need javascript
though. Your forms use java script to always set a cookie to indicate the
data is being submitted from a form. The server always resets the cookie
in its responses. So if a user goes back to the page then the cookie
isn't set so your handler doesn't do anything (it just responds with a
form with the various values as defaults).

I used a serial number scheme once. One problem was that a cached form
will have an old serial number so the user gets an error if they try to
resubmit the data (easy to recover from but annoying). Using cookies
(without java script, but just to track the last serial number) might have
been better. (Your cart probably needs cookies anyway).

I'm sure there are other techniques, but those spring to mind.

$0.04

--

This space not for rent.
Jul 17 '05 #2

P: n/a
Go a redirect (to the shopping cart page, for instance) after the item
has been added.

Jul 17 '05 #3

P: n/a
That's it, clean and simple. I've came up with the lock field in cart object
(lock after update, unlock on pages where user can browse items), but I like
this one better, thanks.

Best regards,

Brko
Jul 17 '05 #4

P: n/a
Thank you, I came up with something (I've described it in reply to chung
leong) but I'll try these too.

Best regards,

Brko
Jul 17 '05 #5

P: n/a
B.r.K.o.N.j.A. wrote:
I've made a script for managing shoppingcart (something like
cart.php?action=add&item=12345) and it works fine, the problem is: when user hits the back button page after he added something in the cart, the add cart script is rerun and he ends up with having 2 of the same item.
That's really a good feature; the user might want to do that. To
handle that cleanly, the item id has to be pushed into the hash;
something like CART[itemid] = count.
Is there a
way to prevent script from running when accessed via back button?


Avoiding back button by redirecting and other means is kind of
breaking user friendlines. I wouldn't want to browse such site.

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

Jul 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.