1

I'm trying to determine the URL of the request caller in a Rails controller.

I tried using request.env['HTTP_REFERER'] but it's sometimes null. Any ideas?

Thanks in advance.

Nonos
  • 2,450
  • 2
  • 23
  • 34
  • Are you sure it's a bug? I believe, referer may be null if a user opens a page from bookmarks or by typing it's address. – bassneck Dec 15 '11 at 00:24

2 Answers2

3

This answer covers the particulars of HTTP_REFERER in Rails

Community
  • 1
  • 1
jdl
  • 17,702
  • 4
  • 51
  • 54
0

You're just going to have to deal with the fact that sometimes there will be no requesting URL. as a commenter said, if a user types the URL in or if it's a bookmark, the referrer field will be empty. And those are just legitimate, everyday cases.

Don't depend on that field being there.

Srdjan Pejic
  • 8,152
  • 2
  • 28
  • 24
  • Thank you all for your help. Is there any other way I can know the caller url, I need to reject requests if coming from certain urls is that possible in Rails? – Nonos Dec 15 '11 at 15:45
  • If you're looking to block URLs, why don't you block their IP addresses instead? The IP address is always available. This is also not a completely reliable method, since the offending URL can change its IP, but it's better than blocking URLs themselves. – Srdjan Pejic Dec 15 '11 at 17:08
  • I can use the ip address but I'm trying to solve a simple problem where I need to accept requests only if they come from a certain URL. I used the following to get the host address: host = request.env['REMOTE_HOST'] if host.blank? host = Resolv.new.getname( request.env['REMOTE_ADDR']) end I couldn't find out whether the value of request.env['REMOTE_ADDR'] can be blank or it has to be set – Nonos Dec 16 '11 at 18:38