add item to shopping cart problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Josef Blösl

    #1

    add item to shopping cart problem

    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

    For affordable website hosting packages, go to MySite.com. You'll find complete and reliable website hosting with a range of prices and options.


    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

    #2
    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.

    Comment

    • Josef Blösl

      #3
      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

      Comment

      • dracolytch

        #4
        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

        Comment

        • dracolytch

          #5
          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

          Comment

          • dracolytch

            #6
            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

            Comment

            • Josef Blösl

              #7
              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)

              Comment

              • dracolytch

                #8
                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

                Comment

                • dracolytch

                  #9
                  Re: add item to shopping cart problem

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

                  header('locatio n: 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

                  Comment

                  • JDS

                    #10
                    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("Locatio n: 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/

                    Comment

                    • R. Rajesh Jeba Anbiah

                      #11
                      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

                      Comment

                      • Tony

                        #12
                        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



                        Comment

                        • richard

                          #13
                          Re: add item to shopping cart problem

                          On 2005-07-19, Josef Blösl <josef.bloesl@g mx.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

                          Comment

                          Working...