Back button and get request...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • B.r.K.o.N.j.A.

    Back button and get request...

    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


  • Malcolm Dew-Jones

    #2
    Re: Back button and get request...

    B.r.K.o.N.j.A. (thebrkonja@ine t.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.

    Comment

    • Chung Leong

      #3
      Re: Back button and get request...

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

      Comment

      • B.r.K.o.N.j.A.

        #4
        Re: Back button and get request...

        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


        Comment

        • B.r.K.o.N.j.A.

          #5
          Re: Back button and get request...

          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


          Comment

          • R. Rajesh Jeba Anbiah

            #6
            Re: Back button and get request...

            B.r.K.o.N.j.A. wrote:[color=blue]
            > I've made a script for managing shoppingcart (something like
            > cart.php?action =add&item=12345 ) and it works fine, the problem is:[/color]
            when user[color=blue]
            > hits the back button page after he added something in the cart, the[/color]
            add cart[color=blue]
            > script is rerun and he ends up with having 2 of the same item.[/color]

            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.
            [color=blue]
            > Is there a
            > way to prevent script from running when accessed via back button?[/color]

            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/

            Comment

            Working...