1

I would like to save the url that user visited right before landing to my site. How should I do it in Rails? I tried request.env["HTTP_REFERER"] but couldn't retrieve the previous url.

May be this is a wrong way to test it, but here is what I did:

  1. I go to a page, say, google.com
  2. Then I type in: localhost:3000. Then has debugger on to catch request.env["HTTP_REFERER"]. But it yields nil

Is HTTP_REFERER only available when user comes to my site via a redirect, and not manually type it the address?

Thank you.

AdamNYC
  • 19,887
  • 29
  • 98
  • 154
  • 3
    It's call `referer`. Have a look here: http://stackoverflow.com/questions/5819721/how-to-get-request-referrer-path – Benjamin M Feb 28 '13 at 22:07

2 Answers2

2

You can't see the last page the user was on if you type a new URL in. HTTP_REFERER is only set when you click on a link. It'd be a pretty big privacy invasion if you could see whatever arbitrary URL the user was previously viewing.

Jim Stewart
  • 16,964
  • 5
  • 69
  • 89
1

This is sent in the HTTP headers as "referer". So, request.referer should have what you need if the user's browser and/or any proxies are not filtering it out.

yogaphil
  • 91
  • 2
  • Thanks. I updated my question to clarify. Could you please advise? – AdamNYC Feb 28 '13 at 22:11
  • 1
    The intent of the referer is to indicate the "source" that led the user to the site. So, clicking a link or getting a redirect might all be valid ways for the last page you were at to be a "referer". If you typed your URL in the browser, you are the "source" not whatever page you are on, so I wouldn't expect the last page visited to show up in the referer header. So, yes, I'd expect it to be empty when you manually type in the address. I'm not sure all browsers conform to this view, and as one of the other posters comment, some now allow you to turn this off for privacy. – yogaphil Feb 28 '13 at 22:55