3

I have a payment page and when the user submits, it captures the payment and directs to a thank you page. The problem is that when the user clicks back, the browser takes them back to the previously submitted page with the payment form and all.

How can i prevent the user from accessing the previous page?

Thanks

Robbo
  • 1,292
  • 2
  • 18
  • 41

1 Answers1

9

@James, put this method in your application controller and call this method on before_action callback like -

before_action :set_cache_buster

and then define the action in protected method like ->

protected

def set_cache_buster
  response.headers["Cache-Control"] = "no-cache, no-store, max-age=0, must-revalidate"
  response.headers["Pragma"] = "no-cache"
  response.headers["Expires"] = "#{1.year.ago}"
end

To accomplish this we just need to disable browser caching using appropriate HTTP headers. Here’s the secret:

Cache-Control: no-cache, max-age=0, must-revalidate, no-store

Taken individually, each of these Cache-Control attributes would seem to prevent caching. In practice, no-cache and no-store are usually interchangeable in most browsers. However for the back button cache specifically, Firefox will only disable this cache if no-store is specified. To play it safe and guarantee cross-browser compatibility, you should use all four attributes.

For more info see the link - Difference between Pragma and Cache-Control headers?

For specific page:

Add that callback on the specific page with the only option like:

before_action :set_cache_buster, only: [:your_action_name]
Jai Chauhan
  • 4,035
  • 3
  • 36
  • 62
Chitra
  • 1,294
  • 2
  • 13
  • 28
  • thanks a lot. Since this is in my app controller won't it affect caching on all pages and not just the payment page? Cheers – Robbo May 24 '15 at 14:56
  • yes, for specific page you can add that filter on that page! – Chitra May 25 '15 at 11:50
  • how exactly is that done for a specific page? should i put it in my custom controller action for that page? – Robbo May 26 '15 at 09:00
  • yea, so i've tried this and it doesn't work locally. i can still hit the back button and view the previous page and the info entered – Robbo May 26 '15 at 09:32
  • Which action should this be applied to? the `GET Checkout` where the form is filled out, the `POST Process` where the transaction data is processed, and/or `GET Thank_You` the post processing page that they shouldn't hit the back button from. – Okomikeruko Dec 13 '19 at 14:47