PHP: Remove URL Param?

前端 未结 6 1859
感情败类
感情败类 2021-01-26 19:38

I have a function that I call with ?clear-cart appended to the end of my page; as you can probably guess, this function clears the user\'s cart.

I call it l

相关标签:
6条回答
  • 2021-01-26 19:44

    Well, in the function that clears the cart, you could redirect the user to the correct URL, using the header() function.

    0 讨论(0)
  • 2021-01-26 19:45

    You can write your cart clearing code in another script and have it redirect back to your page when it's done.

    0 讨论(0)
  • 2021-01-26 19:49

    Something along the lines of this should do what you want:

    if (isset($_GET['clear-cart'])) {
    
      clear_cart();
      header('Location: http://test.local/');
    
    }
    

    Modify to suit your needs.

    0 讨论(0)
  • 2021-01-26 19:49

    Have the page http://test.local/cart link to the page http://test.local/cart?clear-cart, which then redirects the user back to http://test.local/cart.

    /cart -> /cart?clear-cart -> /cart

    if (isset($_GET['clear-cart'])) {
      // Do some cart clearing...
      // Redirect back
      header('Location: /cart');
      exit;   // Very important, otherwise the script will continue until it finally redirects
    }
    
    0 讨论(0)
  • 2021-01-26 20:02

    Instead of a GET-request use a POST-request?

    0 讨论(0)
  • 2021-01-26 20:06

    You could clear the cart and then immediately redirect using header (obviously before any output!).

    <?php
        header('Location: http://test.local/cart');
        ... clear the cart ...
    ?>
    

    See the PHP reference manual for more details.

    0 讨论(0)
提交回复
热议问题